【问题标题】:Set background color for subplot设置子图的背景颜色
【发布时间】:2014-06-12 09:01:36
【问题描述】:

假设我有一个由 4 个子图组成的图像,如下所示:

将 matplotlib.pyplot 导入为 plt 将 numpy 导入为 np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

plt.show()

返回这个:

我想为其中一个图添加灰色背景,比如左下角,因为我喜欢R 使某些图像看起来的方式(例如,参见here)。我还没有找到使用matplotlib 的简单方法,我错过了什么吗?

【问题讨论】:

    标签: python r matplotlib


    【解决方案1】:

    您只需执行axarr[1,0].set_facecolor('grey') 即可手动更改任何特定轴的轴颜色。

    matplotlib 接受许多不同颜色的字符串(例如 herehere)以及 HTML 字符串中的 hex 值(例如 '#eeefff')。

    【讨论】:

    • 谢谢先生。更新函数名称已从 'set_axis_bgcolor' 替换为 'set_facecolor' link 也感谢@nrflaw
    【解决方案2】:

    Axes.set_axis_bgcolor() 自 2.0 版以来已被弃用。立即使用set_facecolor()

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 2018-07-15
      • 2014-09-04
      • 2010-11-10
      相关资源
      最近更新 更多