设置子图的背景色 [英] Set background color for subplot

查看:64
本文介绍了设置子图的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have an image made of 4 sub plots like so:

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

import matplotlib.pyplot as plt import numpy as 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 使某些图像看起来像的方式(请参见此处).我还没有找到使用 matplotlib 做到这一点的简单方法,我缺少什么了吗?

I want to add a grey background to one of this plots, say the bottom left one, since I like the way R makes some images look (see here for example). I haven't found an easy way to do this with matplotlib, am I missing something?

推荐答案

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

You can simply do axarr[1,0].set_facecolor('grey') to change the axis color for any particular axis manually.

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

matplotlib accepts many different color strings (examples here and here) as well as hex values in HTML strings (for example '#eeefff').

这篇关于设置子图的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆