饼图的变种有很多种,下面给出两种常见的变种及其代码示例。
使用Python的Matplotlib库可以很方便地绘制环形图,以下是一个示例代码:
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 创建一个figure对象和一个子图
fig, ax = plt.subplots()
# 绘制饼图
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
# 绘制环形
circle = plt.Circle((0, 0), 0.6, color='white')
ax.add_artist(circle)
# 设置图表标题
ax.set_title('Donut Chart')
# 显示图表
plt.show()
使用Python的Matplotlib库的mplot3d模块可以绘制三维饼图,以下是一个示例代码:
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 创建一个figure对象和一个三维子图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 绘制三维饼图
ax.pie(sizes, labels=labels, autopct='%1.1f%%')
# 设置图表标题
ax.set_title('3D Pie Chart')
# 显示图表
plt.show()
以上是两种饼图的常见变种及其代码示例。根据具体的需求,可以进一步调整代码和参数以满足更多的变种需求。
下一篇:饼图的标签是字符串的问题