Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting figure with matplotlib and alpha value

I am plotting two intersecting transparent histograms with the code below. When I look at the figure, which pops up when I run the code in iPython, everything looks as expected. When I export this figure in png-format, everything is fine as well, but when I export it in eps-format, the transparency is gone and I cannot see the intersecting part of the histograms. I would like to export this in eps-format with transparency. Any advice would be appreciated.

import numpy
from matplotlib import pyplot as plt

d1 = numpy.random.normal(-0.2, 0.25, 5000)
d2 = numpy.random.normal(0.2, 0.25, 5000)
bins = numpy.linspace(-1,1,30)
fig = plt.figure(1,figsize=(30.0, 15.0))
plt.ion()
plt.hist(d1, bins, alpha=0.5, normed=1)
plt.hist(d2, bins, alpha=0.5, normed=1)
plt.show()
plt.savefig('myfig.eps')    # <-- loses transparency
plt.savefig('myfig.png')    # <-- preserves transparency
like image 925
Nras Avatar asked Oct 27 '25 07:10

Nras


1 Answers

You can rasterize the figure before saving it to preserve transparency in the eps file:

ax.set_rasterized(True)
plt.savefig('rasterized_fig.eps')

For more possible solutions see here: Matplotlib Plots Lose Transparency When Saving as .ps/.eps

like image 178
zinjaai Avatar answered Oct 28 '25 22:10

zinjaai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!