4

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
Nras
  • 4,251
  • 3
  • 25
  • 37
  • 1
    EPS has [limited support for transparency](https://en.wikipedia.org/wiki/Transparency_(graphic)#Transparency_in_PostScript), so I would try to avoid using EPS if possible. Would PDF be acceptable? – ford Aug 13 '14 at 12:02
  • 1
    Since the plot is used for a latex-document, PDF is acceptable and also preserves transparency. I just didn't think about it as something similar worked for me in Matlab. Anyway, this solves the problem and keeps my graphic vectorized. Thanks! – Nras Aug 13 '14 at 12:16
  • Possible duplicate of [Matplotlib Plots Lose Transparency When Saving as .ps/.eps](https://stackoverflow.com/questions/19638773/matplotlib-plots-lose-transparency-when-saving-as-ps-eps) – Marcelo Villa-Piñeros Apr 20 '19 at 05:24

1 Answers1

3

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

Community
  • 1
  • 1
zinjaai
  • 2,345
  • 1
  • 17
  • 29