gpt4 book ai didi

python - Matplotlib 饼图 explode 不圆

转载 作者:行者123 更新时间:2023-12-04 16:11:51 26 4
gpt4 key购买 nike

编辑:我无法共享实际数据,但这里有一个示例数据框

df = pd.DataFrame({'education_grouped': ['None', 'Primary', 'Secondary', 
'Post-secondary', 'Bachelor', 'Master', 'Doctoral', 'None', 'Primary',
'Secondary', 'Post-secondary', 'Bachelor', 'Master', 'Doctoral', 'None',
'Primary', 'Secondary', 'Post-secondary', 'Bachelor', 'Master', 'Doctoral']})
我正在使用以下代码制作 donut 饼图
sizes = df['education_grouped'].value_counts().sort_index() / df['education_grouped'].value_counts().sum() * 100
educ_order2 = ['None', 'Primary', 'Secondary', 'Post-secondary', 'Bachelor', 'Master', 'Doctoral']

cmap = plt.get_cmap("Set2")
colors = cmap(np.array([1, 2, 3, 4, 5, 6, 7]))
explode = (0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05)

fig1, ax1 = plt.subplots()
ax1.pie(sizes,
labels=educ_order2,
startangle=90,
explode=explode,
colors=colors,
counterclock=False,
shadow=False,
wedgeprops={'edgecolor': 'white'},
textprops={'fontsize': 7},
pctdistance=0.8,
autopct='%1.1f%%')

centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

ax1.axis('equal')
plt.title('Educational attainment', fontsize=16, pad=20)
plt.tight_layout()
但是,我对它不再显示为圆圈这一事实感到困扰。见下文,似乎有些部分比其他部分移动得更远。这对我来说似乎很奇怪,因为我以相同的数量 explode 了所有切片。有没有人对这里发生的事情有任何暗示?
enter image description here

最佳答案

最好关闭 explode 参数。将其设为零可消除不等式。相比之下,您可能想要适应 radius

sizes = df['education_grouped'].value_counts().sort_index() /    df['education_grouped'].value_counts().sum() * 100
educ_order2 = ['None', 'Primary', 'Secondary', 'Post-secondary', 'Bachelor', 'Master', 'Doctoral']

cmap = plt.get_cmap("Set2")
colors = cmap(np.array([1, 2, 3, 4, 5, 6, 7]))

fig1, ax1 = plt.subplots(figsize=(8,12))
ax1.pie(sizes,
labels=educ_order2,
radius=1,
explode=[0, 0, 0, 0, 0, 0, 0],
startangle=90,
colors=colors,
counterclock=False,
shadow=False,
wedgeprops={'edgecolor': 'white', 'linewidth': 4},
textprops={'fontsize': 7},
pctdistance=0.85,
autopct='%1.1f%%')

centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

ax1.axis('equal')
plt.title('Educational attainment', fontsize=16, pad=20)
plt.tight_layout()
如果你把其中一个放得高一点,例如。 explode=[0.2, 0, 0, 0, 0, 0, 0] ,它为其中一个馅饼提供了很好的特殊效果。该参数可能旨在突出显示一个或几个分数。

关于python - Matplotlib 饼图 explode 不圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65074529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com