8
votes

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

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()

which returns this:

enter image description here

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?

2

2 Answers

12
votes

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

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

6
votes

Axes.set_axis_bgcolor() has been deprecated since version 2.0. Use set_facecolor() now.