Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background color for subplot

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?

like image 932
Gabriel Avatar asked Apr 26 '14 16:04

Gabriel


2 Answers

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

like image 51
Ffisegydd Avatar answered Sep 29 '22 14:09

Ffisegydd


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

like image 28
nrflaw Avatar answered Sep 29 '22 15:09

nrflaw