Can someone please explain why the code below does not work when setting the facecolor of the figure?
import matplotlib.pyplot as plt
# create figure instan
savefig
has its own parameter for facecolor
.
I think an even easier way than the accepted answer is to set them globally just once, instead of putting facecolor=fig.get_facecolor()
every time:
plt.rcParams['axes.facecolor']='red'
plt.rcParams['savefig.facecolor']='red'
If you want to change background color, try this:
plt.rcParams['figure.facecolor'] = 'white'
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)
Hope that helps!
I had to use the transparent keyword to get the color I chose with my initial
fig=figure(facecolor='black')
like this:
savefig('figname.png', facecolor=fig.get_facecolor(), transparent=True)