如何在 Matplotlib 中为条形图和楔形图添加纹理?

matplotlibpythondata visualization更新于 2025/6/6 13:00:17

要为条形图和楔形图添加纹理,我们可以采取以下步骤 −

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 figure() 方法创建新图形或激活现有图形。

  • 将轴添加到图形作为子图布置的一部分。

  • 制作阴影列表。条形图可以填充一些阴影。

  • 创建与阴影数量相等的数字条形图。

  • 使用 bar() 方法绘制具有不同阴影的条形图。

  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(111)
textures = ["//", "*", "o", "d", "."]

for i in range(len(textures)):
   ax.bar(i, np.random.randint(1, 5), color="green", edgecolor="black", alpha=0.3, hatch=textures[i])

plt.show()

输出


相关文章


有用资源