我试图让我的绘图显示一个图例,其中每个值来自的列都有一个标签。我没有将plt.plot()与pivot步骤分开,但我想知道是否仍然可能有一个图例。一个根本没有出现,如果我加上
plt.plot(df_EPErrorPercentByWeekAndDC.pivot(index='hellofresh delivery week', columns='DC', values='Error Percent'), label='DC')
它只是将该字符串用作每个标签,如果我添加df_EPErrorPercentByWeekAndDC['DC']
,那么它只会在每个图例项中显示它的一个字母。以下是我的代码:
print("### Graphing Error Rates by Week and DC EP ###")
# remove percent sign from percent in place
df_EPErrorPercentByWeekAndDC['Error Percent'] = df_EPErrorPercentByWeekAndDC['Error Percent'].str[:-1].astype(float)
plt.xticks(rotation = 90)
plt.plot(df_EPErrorPercentByWeekAndDC.pivot(index='hellofresh delivery week', columns='DC', values='Error Percent'))
plt.legend()
plt.savefig('EPErrorPercentByWeekAndDC.png', bbox_inches="tight", dpi=500)
plt.close()
我不能共享任何数据,但它是一个数据透视表的格式,其中列具有状态名称,每列都充满了百分比,图表工作正常,但图例不存在
保存旋转框架,然后将图例指定为列:
或者直接在数据透视框上使用^{} ,该数据透视框将自动处理图例:
使用的数据帧和导入示例:
df
:pivoted_df
:相关问题 更多 >
编程相关推荐