In IPython Notebook 3, when I use the Inline
matplotlib backend, the png figures in the browser have a transparent background.
How do I set it to white instead?
Minimal example:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2])
Right click and save the image, the image has a transparent background, I would like it to be white instead.
Update
Tried to set figure.facecolor
in matplotlibrc
but it still displays a transparent png:
import matplotlib
print("facecolor before:")
print(matplotlib.rcParams["figure.facecolor"])
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2])
print("facecolor after:")
print(matplotlib.rcParams["figure.facecolor"])
This code gives as output:
facecolor before:
1.0
facecolor after:
(1, 1, 1, 0)
Assuming the area that was transparent is everything surrounding the ax (subplot) you can try this:
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor='w')
ax.plot([1,2])
If you want to have white background in figures permanently you need to modify you matplotlibrc
file (located in your home folder under .matplotlib\
) changing these parameters:
figure.facecolor = 1
But you can always save you figure automatically with any background you want (independent from what it was when the figure was created) by passing facecolor
:
fig.savefig('filename.png', facecolor='w', transparent=False)
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