假设我有一个由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,我是不是遗漏了什么?
发布于 2014-04-27 00:27:35
您可以简单地执行axarr[1,0].set_facecolor('grey')来手动更改任何特定轴的轴颜色。
matplotlib接受许多不同的颜色字符串(例如here和here)以及hex字符串中的HTML值(例如'#eeefff')。

发布于 2019-02-21 01:35:36
自2.0版以来,Axes.set_axis_bgcolor()已被弃用。现在使用set_facecolor()。
https://stackoverflow.com/questions/23313586
复制相似问题