Matplotlib figure facecolor (background color)
It's because savefig
overrides the facecolor for the background of the figure.
(This is deliberate, actually... The assumption is that you'd probably want to control the background color of the saved figure with the facecolor
kwarg to savefig
. It's a confusing and inconsistent default, though!)
The easiest workaround is just to do fig.savefig('whatever.png', facecolor=fig.get_facecolor(), edgecolor='none')
(I'm specifying the edgecolor here because the default edgecolor for the actual figure is white, which will give you a white border around the saved figure)
How to set opacity of background colour of graph with Matplotlib
If you just want the entire background for both the figure and the axes to be transparent, you can simply specify transparent=True
when saving the figure with fig.savefig
.
e.g.:
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(10))
fig.savefig('temp.png', transparent=True)
If you want more fine-grained control, you can simply set the facecolor and/or alpha values for the figure and axes background patch. (To make a patch completely transparent, we can either set the alpha to 0, or set the facecolor to 'none'
(as a string, not the object None
!))
e.g.:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.patch.set_facecolor('blue')
fig.patch.set_alpha(0.7)
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.patch.set_facecolor('red')
ax.patch.set_alpha(0.5)
# If we don't specify the edgecolor and facecolor for the figure when
# saving with savefig, it will override the value we set earlier!
fig.savefig('temp.png', facecolor=fig.get_facecolor(), edgecolor='none')
plt.show()
Matplotlib figure facecolor alpha while saving (background color, transparency)
I ran your code on Matplotlib 1.5 and found that it produced the expected output for me. For all happening on upon this in the future I give two concise ways of achieving this below.
A quick note, you definitely don't want to set transparent=True
as an option for savefig as this will override the facecolors as you can see in the matplotlib.figure.savefig source.
To actually solve your problem the second link you posted How to set opacity of background colour of graph wit Matplotlib actually solves the problem. The issue with the code snippet in the question is the use of fig.set_facecolor
as opposed to fig.patch.set_facecolor
Fix 1:
From the above linked question use the facecolor argument to savefig
import matplotlib.pyplot as plt
fig = plt.figure()
fig.patch.set_facecolor('b') # instead of fig.patch.set_facecolor
fig.patch.set_alpha(0.5)
plt.plot([1,3], [1,3])
plt.tight_layout()
plt.show()
plt.savefig('method1.png', facecolor=fig.get_facecolor())
Fix 2:
You can also specify the savefig facecolor through the rcParams.
import matplotlib.pyplot as plt
import matplotlib as mpl
fig = plt.figure()
col = 'blue'
#specify color of plot when showing in program.
#fig.set_facecolor(col) also works
fig.patch.set_facecolor(col)
#specify color of background in saved figure
mpl.rcParams['savefig.facecolor'] = col
#set the alpha for both plot in program and saved image
fig.patch.set_alpha(0.5)
plt.plot([1,3], [1,3])
plt.tight_layout()
plt.show()
plt.savefig('method2.png')
If you you would like your axes to have a background these solution should leave that background (such as produced by seaborn in Erotemic's comment) intact. If you want to be more explicit about it add:
ax.patch.set_color('palegoldenrod') # or whatever color you like
ax.patch.set_alpha(.7)
The axis patch alpha will transfer to savefig without extra effort.
Note that in both cases I used plt.tight_layout()
to eliminate useless extra space in the saved figure. You can read more on that in the matplotlib documentation.
Using plt.show() how can I define the background color?
Like any other patch, the figure itself has a set_facecolor()
method.
fig = plt.figure()
fig.set_facecolor("w")
You may also directly use the facecolor
argument of plt.figure()
fig = plt.figure(facecolor="w")
A third option is to set the respective rc Parameter. This can either be done in the top of the script
plt.rcParams["figure.facecolor"] = "w"
or by chaning the matplotlib rc file.
Note that from matplotlib version 2.0 on, the default facecolor for figures is actually white, so updating matplotlib is the fourth option available.
Matplotlib: Background color not saved
You have to specify the face color when saving the figure as well, i.e.
plt.savefig("files/images/processed/" + self.get_league_name() + "_" + self.get_competition_name() + ".png", facecolor=self.background_color, dpi = 300)
plt.show()
Matplotlib - savefig ignores facecolor when saving with transparency
It looks like you can achive the desired output via
ax1.set_facecolor((1,1,1,0))
ax2.set_facecolor("grey")
fig.savefig(__file__+".pdf", facecolor=(1,1,1,0))
How can I set the background color on specific areas of a pyplot figure?
You can use axhspan
and/or axvspan
like this:
import matplotlib.pyplot as plt
plt.figure()
plt.xlim(0, 5)
plt.ylim(0, 5)
for i in range(0, 5):
plt.axhspan(i, i+.2, facecolor='0.2', alpha=0.5)
plt.axvspan(i, i+.5, facecolor='b', alpha=0.5)
plt.show()
Related Topics
How to Replace Back Slash Character with Empty String in Python
Tab Completion in Python's Raw_Input()
Python Generator That Groups Another Iterable into Groups of N
How to Combine Python Asyncio with Threads
Python Pack() and Grid() Methods Together
How to Run a Python Script in a Web Page
How to Find Unused Functions in Python Code
How to Open Multiple Webpages in Separate Tabs Within a Browser Using Selenium-Webdriver and Python
In Python, How Does One Catch Warnings as If They Were Exceptions
How to Print Utf-8 Encoded Text to the Console in Python < 3
Rect Collision with List of Rects
Create a Main Loop with Tkinter
What Is the Most Efficient Way of Counting Occurrences in Pandas
How to Add Hours to Current Time in Python
When Should I Be Using Classes in Python
Reference Requirements.Txt for the Install_Requires Kwarg in Setuptools Setup.Py File