1

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

1 回答 1

1

你正在以错误的方式去做。Plotnine 最适用于整洁的数据,即每个变量是一列,每个观察值是一行。否则,您可能最终会与绘图系统发生冲突。

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
    )

在此处输入图像描述

于 2020-07-15T21:30:50.280 回答