如何在ggplot / plotnine中为多条曲线添加图例

时间:2020-07-14 17:29:20

标签: python python-ggplot plotnine

这是我用来绘制两条曲线的示例代码。如何将图例添加到地块?我看到了一些建议在 aes 中添加颜色的帖子,但这会引发异常

plotnine.exceptions.PlotnineError:“无法评估“颜色”映射:“红色”(原始错误:未定义名称“红色”)”

from plotnine import *
import numpy as np
import pandas as pd

str_metric = 'metric'
metric = np.array([0.127, 0.1715, 0.19166667, 0.21583333, 0.24866667, 0.24216667, 0.24433333,
                   0.255, 0.291, 0.30966667, 0.32033333, 0.2415, 0.33833333, 0.30583333, 0.34433333])

metric2 = metric * 2

iterations2 = [i for i in range(len(metric))]


df = pd.DataFrame({'iterations': iterations2,
                   str_metric: metric,
                   str_metric + '2': metric2})

p = ggplot(df, aes(x='iterations')) + geom_smooth(aes(y=metric), color='blue', show_legend=True, method='lm', span=0.10, se=True,
                                                  level=0.80) + geom_smooth(aes(y=metric2), color='red', show_legend=True, method='lm', span=0.10, se=True, level=0.80)
ggsave(p, filename='stackoverflow.png', path='plots/')

1 个答案:

答案 0 :(得分:1)

您正在以错误的方式进行操作。 Plotnine最适合tidy data,即每个变量是一列,每个观察值是一行。否则,您可能最终会与绘图系统抗争。

from plotnine import *
import numpy as np
import pandas as pd

str_metric = 'metric'
metric = np.array([0.127, 0.1715, 0.19166667, 0.21583333, 0.24866667, 0.24216667, 0.24433333,
                   0.255, 0.291, 0.30966667, 0.32033333, 0.2415, 0.33833333, 0.30583333, 0.34433333])

metric2 = metric * 2

iterations2 = [i for i in range(len(metric))]

# tidy data
df = pd.DataFrame({
    'iterations': np.hstack([iterations2, iterations2]),
    'value': np.hstack([metric, metric2]),
    'type': np.repeat(['metric', 'metric2'], len(iterations2))   
})

p = (ggplot(df, aes(x='iterations', y='value', color='type'))
     + geom_smooth(method='lm', span=0.10, se=True, level=0.80)
     # Then you can change the colour using a scale
    )

enter image description here