Matplotlib饼图/甜甜圈图注释文本大小 [英] Matplotlib pie/donut chart annotation text size
问题描述
对于下面给出的从matplotlib官方页面创建甜甜圈图的代码( https://matplotlib.org/3.1.1/gallery/pie_and_polar_charts/pie_and_donut_labels.html )
For the code given below to create a donut chart from matplotlib official page(https://matplotlib.org/3.1.1/gallery/pie_and_polar_charts/pie_and_donut_labels.html)
recipe = ["225 g flour",
"90 g sugar",
"1 egg",
"60 g butter",
"100 ml milk",
"1/2 package of yeast"]
data = [225, 90, 50, 60, 100, 5]
wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
bbox=bbox_props, zorder=0, va="center")
for i, p in enumerate(wedges):
ang = (p.theta2 - p.theta1)/2. + p.theta1
y = np.sin(np.deg2rad(ang))
x = np.cos(np.deg2rad(ang))
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
kw["arrowprops"].update({"connectionstyle": connectionstyle})
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment, **kw)
ax.set_title("Matplotlib bakery: A donut")
plt.show()
我知道那条线
ax.annotate(recipe[i],xy(x,y),xytext(1.35*np.sign(x),1.4*y),horizontalalignment=horizontalalignment, **kw)
定义了注解,但是我无法理解如何控制注解文本的大小.
defines tha annotation, however I am not able to understand how to control the size of the annotation text.
推荐答案
您正确地将该行标识为定义注释.从 ax.annotate
,我们可以看到将额外的关键字传递给 matplotlib.Text
对象.该对象接受fontsize
kwarg,它可以是{size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
之一.
You're correct in identifying that line as defining the annotation. From the documentation for ax.annotate
, we can see that extra keywords are passed to a matplotlib.Text
object. This object accepts a fontsize
kwarg, which can be one of {size in points, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
.
例如:
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment,
fontsize=8, **kw)
输出:
或带有较大的文本:
ax.annotate(recipe[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment,
fontsize=16, **kw)
输出:
这篇关于Matplotlib饼图/甜甜圈图注释文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!