CodeGuyJr
CodeGuyJr

Reputation: 89

How to change matlibplot background color (when using imshow)

I want to change the background color of a plot created with imshow. However, the way to change the background only works based on the figure object

(Matplotlib figure facecolor (background color))

...i.e., you need to use the figure object name: e.g.,

rect.set_facecolor('red')

I have read that imshow creates a figure automatically.

(matplotlib plot and imshow)

Therefore, how can I tell what the automatically-created figure's name is, so that I can use set_facecolor( )

Upvotes: 2

Views: 15245

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339200

Using pyplot you can create the figure by calling any plotting function. I.e.

import matplotlib.pyplot as plt
plt.imshow(data)
plt.show()

creates a figure and shows it.

In this case you may change the figure background color via

plt.gcf().set_facecolor("red")

However it is often useful to create the figure explicitely:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.imshow(data)
fig.set_facecolor("red")
plt.show()

Upvotes: 6

Related Questions