【发布时间】:2020-08-26 12:22:06
【问题描述】:
我想知道我可以在饼图中添加标注。 我有这个数据集:
ID Colour
0 27995 red
1 36185 orange
2 57204 blue
3 46009 red
4 36241 white
5 63286 blue
6 68905 blue
7 3798 green
8 53861 yellow
...
199 193 brown
当我尝试使用 pandas 生成饼图时:
df.groupby(Colour).size().plot(kind='pie',figsize=(15,15), label="", labeldistance=0.8, autopct='%1.0f%%', pctdistance=0.5,legend=True)
我得到一个糟糕的图表,其中颜色重叠,因为切片非常小并且百分比值也重叠。 我知道按以下方式管理图表可能更容易:
How to avoid overlapping of labels & autopct in a matplotlib pie chart?
但在我的情况下,我无法使用答案中的代码。
你能告诉我我应该在那个答案中建议的代码中改变什么吗?
【问题讨论】:
-
在
plt.legend(patches, labels, loc='center right', bbox_to_anchor=(-0.1, 1.), fontsize=8)中使用loc='center right' -
请添加更多详细信息,为什么它不适合您?
-
如果您认为另一个答案是您想要的,那么您只需要在您的案例中定义 x、y 和颜色。尝试
s = df.groupby(Colour).size()然后x = s.index、y = s.values、color = x,那么它应该可以工作吗? -
@Ben。 T. 当我收到
(red, None)时,我遇到了color = x的一些问题。 -
@Pygirl,我不知道在我的情况下 x 和 y 是什么字段。
标签: python pandas matplotlib