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

How to add legend in ggplot/plotnine for multiple curves

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

plotnine.exceptions.PlotnineError: "Could not evaluate the 'color' mapping: 'red' (original error: name 'red' is not defined)"

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='Whosebug.png', path='plots/')

你的做法是错误的。 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
    )