当切片很小时避免重叠

sti*_*ing 1 python matplotlib pandas

我想知道我可以在饼图中添加标注吗?我有这个数据集:

       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
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 pandas 生成饼图时:

df.groupby(Colour).size().plot(kind='pie',figsize=(15,15), label="", labeldistance=0.8, autopct='%1.0f%%',  pctdistance=0.5,legend=True)
Run Code Online (Sandbox Code Playgroud)

我得到了一个糟糕的图表,其中颜色重叠,因为切片非常小并且百分比值也重叠。我知道按如下方式管理图表可能会更容易:

如何避免 matplotlib 饼图中标签和 autopct 重叠?

但在我的案例中,我无法使用答案中的代码。

您能告诉我应该在该答案中建议的代码中更改哪些内容吗?

r-b*_*ers 5

在“matplotlib”中微调更容易处理。我以官方参考为例,修改了你的数据。从这里。点是explode=(),它设置要从元组中剪切的切片数。

import matplotlib.pyplot as plt

colours = ['red', 'orange', 'blue', 'white', 'green', 'yellow', 'brown']

labels = colours
sizes = df.groupby('Colour').size().tolist()
# only "explode" the 3nd,4th slice (i.e. 'Blue, White')
explode = (0, 0, 0.2, 0.2, 0, 0, 0)

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90, colors=colours)

# Equal aspect ratio ensures that pie is drawn as a circle.
ax1.axis('equal')  

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述