16

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 Answers 2

23

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sir. Update the function name has been replaced from 'set_axis_bgcolor' to 'set_facecolor' link Also thanks @nrflaw
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.