备注
前往结尾 下载完整示例代码。
一个带有标签的派和一个甜甜圈#
欢迎来到 Matplotlib 烘焙店。我们将通过 pie 方法 创建一个饼图和一个甜甜圈图,并展示如何使用 图例 以及 注释 来标注它们。
像往常一样,我们首先定义导入并创建一个带有子图的图形。现在是时候画饼图了。从制作一个饼图的配方开始,我们根据数据创建数据和标签列表。
我们可以为 autopct 参数提供一个函数,该函数将通过显示绝对值来扩展自动百分比标签;我们根据相对数据和所有值的总和来计算后者。
然后我们创建饼图并存储返回的对象以供后续使用。返回元组的第一个元素是楔形列表。这些是 matplotlib.patches.Wedge 补丁,可以直接用作图例的句柄。我们可以使用图例的 bbox_to_anchor 参数将图例放置在饼图外部。这里我们使用坐标系 (1, 0, 0.5, 1) 和位置 "center left";即图例的左中心点将位于边界框的左中心点,在坐标系中从 (1, 0) 到 (1.5, 1) 延伸。
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
recipe = ["375 g flour",
"75 g sugar",
"250 g butter",
"300 g berries"]
data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe]
def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d} g)"
wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w"))
ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax.set_title("Matplotlib bakery: A pie")
plt.show()

现在是甜甜圈的时间了。从甜甜圈的食谱开始,我们将数据转换为数字(将1个鸡蛋转换为50克),并直接绘制饼图。饼图?等等……它不是要变成甜甜圈吗?好吧,正如我们在这里看到的,甜甜圈是一个饼图,具有一定的``width``设置给楔形,这与它的半径不同。这非常简单。这是通过``wedgeprops``参数完成的。
然后我们希望通过 annotations 来标记楔形。我们首先创建一些字典来存储常用属性,这些属性稍后可以作为关键字参数传递。然后我们遍历所有楔形,并对每个
计算楔形中心的角度,
从那里获取该角度在圆周上的点的坐标,
根据点在圆的哪一侧,确定文本的水平对齐方式,
使用获得的角更新连接样式,使标注箭头从圆环向外指向,
最后,使用所有之前确定的参数创建注释。
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
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 = f"angle,angleA=0,angleB={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()

这就是甜甜圈。不过请注意,如果我们按照这个食谱来做,这些材料大约可以制作6个甜甜圈——制作一个巨大的甜甜圈未经测试,可能会导致厨房失误。
参考文献
本示例展示了以下函数、方法、类和模块的使用: