2

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

1 Answer 1

7

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()
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot @ImportanceOfBeingErnest
@CodeGuyJr Can you mark his answer as the correct answer? Because then other people can mark similar questions as duplicates of this one
@zwep Note that all that's needed for close-voting a question as duplicate is that the dupe-target has an answer and that answer is either upvoted or accepted. Hence if you want to close-vote a question, you may just upvote one of the target's answers to be able to do so.
@ImportanceOfBeingErnest Upvoted it. Thanks for the instruction; I am a noob here.
@Bazingaa Well, not out of disregard, but simply because it wasn't apparent to me that "mark correct" = click that little check mark. I just "upvoted" something and Stackoverflow prompted me to click the green arrow. Now I know.
|

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.