基本饼图

演示一个基本的饼图和一些额外的功能。

除了基本饼图外,此演示还显示了以下几个可选功能:

  • 切片标签。
  • 自动标记百分比。
  • explode 偏移切片。
  • 投影。
  • 自定义起始角度

请注意,自定义起点角度:

默认的起始角度(startangle)为0,这将在正x轴上开始“Frogs”切片。此示例将 startangle设置为90 ,以便将所有对象逆时针旋转90度,并且青蛙切片从正y轴开始。

  1. import matplotlib.pyplot as plt
  2. # Pie chart, where the slices will be ordered and plotted counter-clockwise:
  3. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
  4. sizes = [15, 30, 45, 10]
  5. explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
  6. fig1, ax1 = plt.subplots()
  7. ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
  8. shadow=True, startangle=90)
  9. ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
  10. plt.show()

基本饼图示例

参考

此示例显示了以下函数、方法、类和模块的使用:

  1. import matplotlib
  2. matplotlib.axes.Axes.pie
  3. matplotlib.pyplot.pie

下载这个示例