备注
前往结尾 下载完整示例代码。
Hatch 演示#
在 Matplotlib 中,可以在大多数多边形上添加阴影,包括 bar、fill_between、contourf 以及 Polygon 的子类。目前,PS、PDF、SVG、macosx 和 Agg 后端支持阴影。WX 和 Cairo 后端目前不支持阴影。
另请参阅 等值线填充图案 以获取使用 contourf 的示例,以及 Hatch 样式参考 以获取现有阴影图案的样本。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse, Polygon
x = np.arange(1, 5)
y1 = np.arange(1, 5)
y2 = np.ones(y1.shape) * 4
fig = plt.figure()
axs = fig.subplot_mosaic([['bar1', 'patches'], ['bar2', 'patches']])
axs['bar1'].bar(x, y1, edgecolor='black', hatch="/")
axs['bar1'].bar(x, y2, bottom=y1, edgecolor='black', hatch='//')
axs['bar2'].bar(x, y1, edgecolor='black', hatch=['--', '+', 'x', '\\'])
axs['bar2'].bar(x, y2, bottom=y1, edgecolor='black',
hatch=['*', 'o', 'O', '.'])
x = np.arange(0, 40, 0.2)
axs['patches'].fill_between(x, np.sin(x) * 4 + 30, y2=0,
hatch='///', zorder=2, fc='c')
axs['patches'].add_patch(Ellipse((4, 50), 10, 10, fill=True,
hatch='*', facecolor='y'))
axs['patches'].add_patch(Polygon([(10, 20), (30, 50), (50, 10)],
hatch='\\/...', facecolor='g'))
axs['patches'].set_xlim([0, 40])
axs['patches'].set_ylim([10, 60])
axs['patches'].set_aspect(1)
plt.show()

参考文献
以下示例展示了以下函数、方法、类和模块的使用: