如何在 Matplotlib 中更改图框的颜色?

matplotlibserver side programmingprogramming更新于 2025/4/29 7:37:17

要更改图框的颜色,我们可以将轴刻度线和脊柱值设置为特定颜色。

步骤

  • 创建一个图形并添加一组子图,使用 subplots  方法,值为 4。

  • 将颜色与轴压缩并一起迭代。

  • 在迭代中,设置脊柱值和刻度线(x,y)的颜色。

  • 调整子图之间和周围的填充。

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

示例

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4)
for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'red', 'yellow', 'blue']):
   plt.setp(ax.spines.values(), color=color)
   ax.plot([8, 3], c=color)
   plt.setp([ax.get_xticklines(), ax.get_yticklines()], color=color)
plt.show()

输出


相关文章