Matplotlib change background color inner and outer colorset_facecolor() method is used to change the inner background color of the plot. figure(facecolor='color') method is used to change the outer background color of the plot. In the above example, we have change both an inner and outer color of background of plot.
Direct link to this answer Approach 1: a) From the File menu, select "Export setup". b) Under Properties select Rendering and check the “Custom color” option. c) Enter “w” in the adjacent text box and click “Apply to Figure” to update the figure.
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)
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With