一个饼图和一个甜甜圈图,带有标签#

欢迎来到Matplotlib烘焙坊。我们将通过pie方法创建饼图和甜甜圈图,并展示如何使用legend以及annotations来为它们添加标签。

通常,我们首先定义导入并创建一个带子图的图形。现在是制作饼图的时候了。从饼图的“食谱”开始,我们创建数据并从中生成标签列表。

我们可以为autopct参数提供一个函数,它将通过显示绝对值来扩展自动百分比标签;我们根据相对数据和所有值的已知总和反向计算出绝对值。

然后我们创建饼图并存储返回的对象以备后用。返回元组的第一个元素是扇形块(wedge)的列表。这些是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()
Matplotlib bakery: A pie

现在轮到甜甜圈图了。从甜甜圈的“食谱”开始,我们将数据转换为数字(将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()
Matplotlib bakery: A donut

这就是甜甜圈图。然而请注意,如果使用此食谱,配料足够制作大约6个甜甜圈——制作一个巨大的甜甜圈尚未经过测试,可能会导致厨房错误。

参考

本示例展示了以下函数、方法、类和模块的使用

标签:组件:标签 组件:注解 图表类型:饼图 难度:初级

由 Sphinx-Gallery 生成的画廊