简体   繁体   English

pandas python 图例未显示所有颜色类别

[英]pandas python legend not showing all color categories

I am trying to plot a graph using dataframe values, I have three columns in the dataframe, column one is the legend values of the graph, column two is the x axis and column third is the y-axis.我正在尝试使用 dataframe 值对 plot 进行图形处理,我在 dataframe 中有三列,第一列是坐标轴的图例值,第二列是坐标轴的图例值,第三列是坐标轴。 When I am plotting the graph it is only showing me one category in the legend not all, its showing me only one color当我绘制图表时,它只显示图例中的一个类别而不是全部,它只显示一种颜色

This is my dataframe这是我的 dataframe

在此处输入图像描述

This is my code这是我的代码

ax1 = graphdf.plot(kind='bar', x='Bottles', y='bottle Avg')
figures = graphdf['fig'].tolist()
ax1.legend(labels=figures, fancybox=True, shadow=True)
for p in ax1.patches:
    label1 = "{:.1f}".format(p.get_height())
    ax1.annotate(label1, xy=(p.get_x() * 1.015, p.get_height() * 1.015))

but this is only showing 1 categories in the label not all.但这仅显示 label 中的 1 个类别,而不是全部。

This is the output, I want to show all categories in the legend这是 output,我想在图例中显示所有类别

在此处输入图像描述

In this usage pandas.DataFrame.plot.bar calls matplotlib.pyplot.bar exactly once, creating a single collection of patches with a single label. In this usage pandas.DataFrame.plot.bar calls matplotlib.pyplot.bar exactly once, creating a single collection of patches with a single label. The matplotlib.axes.Axes.legend signature where only the labels are specified simply assigns labels to " existing plot elements ".仅指定标签的matplotlib.axes.Axes.legend签名只是将标签分配给“现有的 plot 元素”。 Since plt.bar creates a single "plot element" only one legend entry is created.由于plt.bar创建了一个“绘图元素”,因此只创建了一个图例条目。 You can easily get around this by using the third call signature for ax.legend , where both the labels and handles are specified, ie您可以通过使用ax.legend的第三个调用签名轻松解决此问题,其中指定标签和句柄,即

ax = df.plot(kind='bar', x='Bottles', y='bottle Avg')
ax.legend(labels=df['fig'].tolist(), handles=ax.patches, fancybox=True, shadow=True)
for p in ax.patches:
    ax.annotate("{:.1f}".format(p.get_height()), xy=(p.get_x() * 1.015, p.get_height() * 1.015))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM