Pie 绘制饼图演示

使用 pie(). 制作饼图。

此示例演示了一些饼图功能,如标签、可变大小、自动标记百分比、偏移切片和添加阴影。

  1. import matplotlib.pyplot as plt
  2. # Some data
  3. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
  4. fracs = [15, 30, 45, 10]
  5. # Make figure and axes
  6. fig, axs = plt.subplots(2, 2)
  7. # A standard pie plot
  8. axs[0, 0].pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
  9. # Shift the second slice using explode
  10. axs[0, 1].pie(fracs, labels=labels, autopct='%.0f%%', shadow=True,
  11. explode=(0, 0.1, 0, 0))
  12. # Adapt radius and text size for a smaller pie
  13. patches, texts, autotexts = axs[1, 0].pie(fracs, labels=labels,
  14. autopct='%.0f%%',
  15. textprops={'size': 'smaller'},
  16. shadow=True, radius=0.5)
  17. # Make percent texts even smaller
  18. plt.setp(autotexts, size='x-small')
  19. autotexts[0].set_color('white')
  20. # Use a smaller explode and turn of the shadow for better visibility
  21. patches, texts, autotexts = axs[1, 1].pie(fracs, labels=labels,
  22. autopct='%.0f%%',
  23. textprops={'size': 'smaller'},
  24. shadow=False, radius=0.5,
  25. explode=(0, 0.05, 0, 0))
  26. plt.setp(autotexts, size='x-small')
  27. autotexts[0].set_color('white')
  28. plt.show()

Pie 绘制饼图演示

参考

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

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

下载这个示例