增加饼图matplotlib中标签的字体大小

2024-05-20 16:45:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个饼图,看起来像:

enter image description here

我尝试使用textprops={'fontsize':18})增加字体大小。但是,它只改变饼图内部百分比标签的字体大小,而外部标签则不受影响。在

我想增加上面饼图中标签A,B,C等的字体大小。在

我的代码:

fig1, ax1 = plt.subplots(figsize=(24,12))
flavor_pie = ax1.pie(data2.Count_Of_labels,labels=['A','B','C','D','E','F'], autopct='%.0f%%', shadow=True, colors=colors, 
                     explode= explode1, startangle= -90, textprops={'fontsize': 18})

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

ax1.axis('equal')  
plt.tight_layout()
plt.show()

Tags: 代码labelsfigplt标签字体大小百分比colors
2条回答

您可能使用的是matplotlib的旧版本;在任何新版本中,标签和autopercentage的大小都相同。在

因此,问题可以归结为如何为标签和自动百分比设置不同的字体大小。在

有这样的饼图吗

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
wedges, labels, autopct = ax.pie([1,2,3,4,3,2],labels=['A','B','C','D','E','F'], 
                                  autopct='%.0f%%', wedgeprops=dict(width=.7))

您可以在标签或autopercentages上循环,并将fontsize设置为

^{pr2}$

或者一次把它们都设置好,比如

plt.setp(labels, fontsize=15)

autopct相似。在

尝试:

import matplotlib as mpl
mpl.rcParams['font.size'] = 18.0

或者

^{pr2}$

或者

import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 18

相关问题 更多 >