Hatch演示

目前仅在PS,PDF,SVG和Agg后端支持阴影线(图案填充多边形)。

  1. import matplotlib.pyplot as plt
  2. from matplotlib.patches import Ellipse, Polygon
  3. fig, (ax1, ax2, ax3) = plt.subplots(3)
  4. ax1.bar(range(1, 5), range(1, 5), color='red', edgecolor='black', hatch="/")
  5. ax1.bar(range(1, 5), [6] * 4, bottom=range(1, 5),
  6. color='blue', edgecolor='black', hatch='//')
  7. ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
  8. bars = ax2.bar(range(1, 5), range(1, 5), color='yellow', ecolor='black') + \
  9. ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5),
  10. color='green', ecolor='black')
  11. ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
  12. patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.')
  13. for bar, pattern in zip(bars, patterns):
  14. bar.set_hatch(pattern)
  15. ax3.fill([1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch='\\')
  16. ax3.add_patch(Ellipse((4, 1.5), 4, 0.5, fill=False, hatch='*'))
  17. ax3.add_patch(Polygon([[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]], closed=True,
  18. fill=False, hatch='/'))
  19. ax3.set_xlim((0, 6))
  20. ax3.set_ylim((0, 2.5))
  21. plt.show()

Hatch演示

参考

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

  1. import matplotlib
  2. matplotlib.patches
  3. matplotlib.patches.Ellipse
  4. matplotlib.patches.Polygon
  5. matplotlib.axes.Axes.add_patch
  6. matplotlib.patches.Patch.set_hatch
  7. matplotlib.axes.Axes.bar
  8. matplotlib.pyplot.bar

下载这个示例