matplotlib 饼状图引线
时间: 2023-07-12 10:13:24 浏览: 158
在 Matplotlib 中,你可以使用 `pie()` 函数创建饼状图,并使用 `annotate()` 函数添加引线。下面是一个简单的例子:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 饼状图
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
# 引线
for i, label in enumerate(labels):
angle = sum(sizes[:i]) + sizes[i]/2
x = 1.4 * np.cos(np.radians(90 - angle))
y = 1.4 * np.sin(np.radians(90 - angle))
ax.annotate(label, xy=(x, y), xytext=(1.5*x, 1.5*y),
fontsize=12, ha='center', va='center',
arrowprops=dict(arrowstyle='-', lw=1))
plt.show()
```
这段代码会创建一个带有引线的饼状图。其中,`angle` 表示每个扇形的中心角度,`x` 和 `y` 表示引线的起点坐标,`xytext` 表示引线的终点坐标,`arrowprops` 控制箭头的样式。你可以根据需要修改这些参数来调整引线的位置和样式。
阅读全文
相关推荐

















