Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set IPython Notebook inline plots background not transparent

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)
like image 891
Andrea Zonca Avatar asked Apr 10 '15 21:04

Andrea Zonca


1 Answers

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)
like image 159
Primer Avatar answered Sep 26 '22 13:09

Primer