I want to have labels with small size on a piechart in python to improve visibility here is the code
我希望python中的饼图上有小尺寸的标签,以提高可视性,这里是代码
import matplotlib.pyplot as plt
frac=[1.40 , 10.86 , 19.31 , 4.02 , 1.43 , 2.66 , 4.70 , 0.70 , 0.13 , 1.48, 32.96 , 1.11 , 13.30 , 5.86]
labels=['HO0900344', 'HO0900331', 'HO0900332', 'HO0900354',
'HO0900358', 'HO0900374', 'HO0900372', 'HO0900373',
'HO0900371', 'HO0900370', 'HO0900369', 'HO0900356',
'HO0900353', 'HO0900343']
fig = plt.figure(1, figsize=(6,6))
ax = fig.add_subplot(111)
ax.axis('equal')
colors=('b', 'g', 'r', 'c', 'm', 'y', 'burlywood', 'w')
ax.pie(frac,colors=colors ,labels=labels, autopct='%1.1f%%')
plt.show()
Thanks and cheers
谢谢和欢呼
2 个解决方案
#1
35
There are a couple of ways you can change the font size of the labels.
您可以通过多种方式更改标签的字体大小。
You can dynamically changet the rc settings. Add the following at the top of your script:
您可以动态更改rc设置。在脚本顶部添加以下内容:
import matplotlib as mpl
mpl.rcParams['font.size'] = 9.0
Or you can modify the labels after they have been created. When you call ax.pie
it returns a tuple of (patches, texts, autotexts). As an example, modify your final few lines of code as follows:
或者您可以在创建标签后对其进行修改。当你调用ax.pie时,它会返回一个元组(补丁,文本,自动缩放)。例如,修改最后几行代码,如下所示:
patches, texts, autotexts = ax.pie(frac, colors=colors, labels=labels, autopct='%1.1f%%')
texts[0].set_fontsize(4)
plt.show()
#2
7
The simplest way to change the font size on a pie chart is directly via the textprops
argument in the pie()
function. Using the code above add it like so:
更改饼图上字体大小的最简单方法是直接通过pie()函数中的textprops参数。使用上面的代码添加如下:
ax.pie(frac, colors=colors ,labels=labels,
autopct='%1.1f%%', textprops={'fontsize': 14})
That way you can just pass in a dictionary with your desired fontsize (e.g., 14). No messing around with rcParams
or return values from the function call.
这样你就可以传入一个包含所需字体大小的字典(例如,14)。不要乱用rcParams或从函数调用中返回值。
#1
35
There are a couple of ways you can change the font size of the labels.
您可以通过多种方式更改标签的字体大小。
You can dynamically changet the rc settings. Add the following at the top of your script:
您可以动态更改rc设置。在脚本顶部添加以下内容:
import matplotlib as mpl
mpl.rcParams['font.size'] = 9.0
Or you can modify the labels after they have been created. When you call ax.pie
it returns a tuple of (patches, texts, autotexts). As an example, modify your final few lines of code as follows:
或者您可以在创建标签后对其进行修改。当你调用ax.pie时,它会返回一个元组(补丁,文本,自动缩放)。例如,修改最后几行代码,如下所示:
patches, texts, autotexts = ax.pie(frac, colors=colors, labels=labels, autopct='%1.1f%%')
texts[0].set_fontsize(4)
plt.show()
#2
7
The simplest way to change the font size on a pie chart is directly via the textprops
argument in the pie()
function. Using the code above add it like so:
更改饼图上字体大小的最简单方法是直接通过pie()函数中的textprops参数。使用上面的代码添加如下:
ax.pie(frac, colors=colors ,labels=labels,
autopct='%1.1f%%', textprops={'fontsize': 14})
That way you can just pass in a dictionary with your desired fontsize (e.g., 14). No messing around with rcParams
or return values from the function call.
这样你就可以传入一个包含所需字体大小的字典(例如,14)。不要乱用rcParams或从函数调用中返回值。