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

    1. import matplotlib.pyplot as plt
    2. # 设置数据
    3. labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
    4. fracs = [15, 30, 45, 10]
    5. # 制作图形和轴
    6. fig, axs = plt.subplots(2, 2)
    7. # 绘制标准饼图
    8. axs[0, 0].pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
    9. # 用explode突出显示第二项
    10. axs[0, 1].pie(fracs, labels=labels, autopct='%.0f%%', shadow=True,
    11. explode=(0, 0.1, 0, 0))
    12. # 调整半径和文字大小绘制
    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. # 使文字更小
    18. plt.setp(autotexts, size='x-small')
    19. autotexts[0].set_color('white')
    20. # 使用较小的突出和关闭阴影以获得更好的可见性
    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()

    image.png