这是我用来绘制两条曲线的示例代码。如何将图例添加到情节中?我看到一些帖子建议在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/')