python饼图指引线
时间: 2023-06-30 16:15:52 浏览: 334
在 Python 中,我们可以使用 `matplotlib` 库来创建饼图以及指引线。以下是一个简单的示例代码,演示如何使用 `matplotlib` 来创建带有指引线的饼图:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 颜色
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
# 创建饼图
fig1, ax1 = plt.subplots()
ax1.pie(sizes, colors=colors, labels=labels, autopct='%1.1f%%', startangle=90)
# 创建指引线
for i, label in enumerate(labels):
angle = (sum(sizes[:i]) + sizes[i]/2) / sum(sizes) * 360
x = 1.2 * np.cos(np.radians(angle))
y = 1.2 * np.sin(np.radians(angle))
if x > 0:
ax1.annotate(label, (x, y), xytext=(1.3, y), fontsize=12, va='center', ha='left')
else:
ax1.annotate(label, (x, y), xytext=(-1.3, y), fontsize=12, va='center', ha='right')
# 设置图形标题
ax1.set_title('Pie Chart with Annotations')
# 显示图形
plt.show()
```
在这个示例中,我们使用 `pie()` 函数创建了饼图,并使用 `autopct` 参数来显示每个部分的百分比。然后,我们使用 `annotate()` 函数创建了指引线,并使用 `xytext` 参数设置指引线的位置。最后,我们使用 `set_title()` 函数设置了图形标题,并使用 `show()` 函数显示了图形。
阅读全文
相关推荐


















