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:
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?
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'
).
Axes.set_axis_bgcolor() has been deprecated since version 2.0. Use set_facecolor() now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With