备注
Go to the end 下载完整的示例代码...
阴影样式参考#
阴影可以添加到 Matplotlib 中的大多数多边形,包括 bar , fill_between , contourf 和 Polygon 的子项.目前 PS,PDF,SVG,macosx 和 Agg 后端支持阴影.WX 和 Cairo 后端目前不支持阴影.
另请参见 Contourf 填充图案 ,其中包含使用 contourf 的示例,以及 阴影演示 ,其中包含更多用法示例.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))
hatches = ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
def hatches_plot(ax, h):
ax.add_patch(Rectangle((0, 0), 2, 2, fill=False, hatch=h))
ax.text(1, -0.5, f"' {h} '", size=15, ha="center")
ax.axis('equal')
ax.axis('off')
for ax, h in zip(axs.flat, hatches):
hatches_plot(ax, h)

可以重复阴影图案以增加密度.
fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))
hatches = ['//', '\\\\', '||', '--', '++', 'xx', 'oo', 'OO', '..', '**']
for ax, h in zip(axs.flat, hatches):
hatches_plot(ax, h)

可以组合阴影图案以创建其他图案.
fig, axs = plt.subplots(2, 5, layout='constrained', figsize=(6.4, 3.2))
hatches = ['/o', '\\|', '|*', '-\\', '+o', 'x*', 'o-', 'O|', 'O.', '*-']
for ax, h in zip(axs.flat, hatches):
hatches_plot(ax, h)

参考
以下函数,方法,类和模块的用法在本例中显示:
matplotlib.patchesmatplotlib.patches.Rectanglematplotlib.axes.Axes.add_patchmatplotlib.axes.Axes.text
脚本的总运行时间:(0 分 1.306 秒)