Python 如何使用matplotlib中的mpatches.Patch向条形图添加自定义标签
我试图手动添加Python 如何使用matplotlib中的mpatches.Patch向条形图添加自定义标签,python,pandas,matplotlib,bar-chart,legend,Python,Pandas,Matplotlib,Bar Chart,Legend,我试图手动添加图例,但它们没有显示。我想使用matplotlib路径而不是seaborn色调,这样我可以控制我的颜色并处理重复产品 Dec = df.event_ID.value_counts().nlargest(10).keys().tolist() text = [Typ[i] for i in Dec] 当我们打印文本输出时 print(text) ['JK)', 'CDE', 'GAMED', 'JK', 'DTA', 'AU365', 'BCD', 'BCD',
图例
,但它们没有显示。我想使用matplotlib路径
而不是seaborn
色调,这样我可以控制我的颜色
并处理重复产品
Dec = df.event_ID.value_counts().nlargest(10).keys().tolist()
text = [Typ[i] for i in Dec]
当我们打印文本输出时
print(text)
['JK)',
'CDE',
'GAMED',
'JK',
'DTA',
'AU365',
'BCD',
'BCD',
'ADD',
'ADD']
这里是颜色和我的方法
color=["peru", "crimson", "magenta", "dimgrey", "forestgreen", "orange",
"dodgerblue", "aliceblue", "gold", "firebrick"]
df.groupby("event_ID")["Typ"].size().nlargest(10).plot(kind="bar", color=color)
plt.legend(color, text)
但是我看不到绘图中的文本您可能需要:
df1=df.groupby("event_ID")["Typ"].size().nlargest(10)
plt.bar(df1.index,df1, color=color,label=text)
plt.legend(loc='upper right')
这将有你的传奇在右上角,但你当然可以自由地改变它。您可以阅读文档以了解更详细的用法。
还要注意
df.groupby('keywords')['number'].size().plot(kind='bar',color=color,label=text)
不起作用,因为,标签
只接受字符串,而不接受列表