gpt4 book ai didi

python - 设置子图的背景颜色

转载 作者:行者123 更新时间:2023-12-02 02:35:21 33 4
gpt4 key购买 nike

假设我有一个由 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()

返回这个:

enter image description here

我想向其中一个图添加灰色背景,例如左下图,因为我喜欢 R 使某些图像看起来的方式(例如,请参见 here)。我还没有找到使用 matplotlib 执行此操作的简单方法,我是否遗漏了什么?

最佳答案

你可以简单地做 axarr[1,0].set_facecolor('grey')手动更改任何特定轴的轴颜色。

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

关于python - 设置子图的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313586/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com