Matplotlib Plots Lose Transparency When Saving as .ps/.eps
Asked Answered
S

8

55

I'm having an issue with attempting to save some plots with transparent ellipsoids on them if I attempt to save them with .ps/.eps extensions.

Here's the plot saved as a .png: png

If I choose to save it as a .ps/.eps here is what it looks like: ps

How I got around this, was to use ImageMagick to convert the original png to a ps. The only problem is that the image in png format is about 90k, and it becomes just under 4M after conversion. This is not good since I have a lot of these images, and it will take too much time to compile my latex document. Does anyone have a solution to this?

Sibelius answered 28/10, 2013 at 15:27 Comment(1)
Did you finally solve the problem without using any mimic?Quixotism
H
37

The problem is that eps does not support transparencies natively.

There are few options:

  1. rasterize the image and embed in a eps file (like @Molly suggests) or exporting to pdf and converting with some external tool (like gs) (which usually relies as well on rasterization)

  2. 'mimic' transparency, giving a colour that looks like the transparent one on a given background.

I discussed this for sure once on the matplotlib mailing list, and I got the suggestion to rasterize, which is not feasible as you get either pixellized or huge figures. And they don't scale very nicely when put into, e.g., a publication.

I personally use the second approach, and although not ideal, I found it good enough. I wrote a small python script that implements the algorithm from this SO post to obtain a solid RGB representation of a colour with a give transparency

EDIT

In the specific case of your plot try to use the zorder keyword to order the parts plotted. Try to use zorder=10 for the blue ellipse, zorder=11 for the green and zorder=12 for the hexbins.

This way the blue should be below everything, then the green ellipse and finally the hexbins. And the plot should be readable also with solid colors. And if you like the shades of blue and green that you have in png, you can try to play with mimic_alpha.py.

EDIT 2

If you are 100% sure that you have to use eps, there are a couple of workarounds that come to my mind (and that are definitely uglier than your plot):

  1. Just draw the ellipse borders on top of the hexbins.
  2. Get centre and amplitude of each hexagon, (possibly discard all zero bins) and make a scatter plot using the same colour map as in hexbin and adjusting the marker size and shape as you like. You might want to redraw the ellipses borders on top of that
Herzig answered 28/10, 2013 at 17:41 Comment(5)
Thanks, I tried your mimic_alpha.py code, but there is no way for you to see through it to the scatterplot I've also generated. The colors are matched nicely, but it's still not transparent.Sibelius
yes, it mimics the colors, but returns standard RGB colors. In any case, if you have to use eps there is not much that can be done. I uses the mimic_alpha a few times (I think mostly with fill_between), enhancing the limits with standard lines on top of the shaded areas.Herzig
@astromax: I've edited the answer adding a possible solution of your problemHerzig
I've also tried that - the problem is even though hexbin has many zero bins, the hexbin plot spans the entire range. In this case hexbin with a zorder larger than the ellipses completely blocks them.Sibelius
@Sibelius I see. It's extremely annoying. I edit the answer with a possible workaroundHerzig
H
32

Another alternative would be to save them to pdf

savefig('myfigure.pdf')

That works with pdflatex, if that was the reason why you needed to use eps and not svg.

Harlequinade answered 11/3, 2014 at 23:13 Comment(1)
Really this is the correct answer. I am not aware of any publications in 2020 that do not accept figures in PDF format.Avalon
D
14

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

ax.set_rasterized(True)
plt.savefig('rasterized_fig.eps')
Donau answered 28/10, 2013 at 16:38 Comment(2)
Very helpful, the only problem is not it's 33M, which is way too large. I have a lot of these and they can't be that big.Sibelius
Also, it seems to be coloring the back of the plot black, so it's covering up the x and y axis labels. Any idea why it's doing that?Sibelius
C
11

I had the same problem. To avoid rasterizing, you can save the image as a pdf and then run (on unixish systems at least) in a terminal:

pdftops -eps my.pdf my.eps

Which gives a .eps file as output.

Curfew answered 8/5, 2014 at 11:47 Comment(0)
C
3

I solved this by: 1) adding a set_rasterization_zorder(1) when defining the figure area:

fxsize=16
fysize=8
f = figure(num=None, figsize=(fxsize, fysize), dpi=180, facecolor='w',
edgecolor='k')
plt.subplots_adjust(
left    = (18/25.4)/fxsize, 
bottom  = (13/25.4)/fysize, 
right   = 1 - (8/25.4)/fxsize, 
top     = 1 - (8/25.4)/fysize)
subplots_adjust(hspace=0,wspace=0.1)
#f.suptitle('An overall title', size=20)
gs0 = gridspec.GridSpec(1, 2)

gs11 = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[0])

ax110 = plt.Subplot(f, gs11[0,0])
f.add_subplot(ax110)

ax110.set_rasterization_zorder(1)

2) a zorder=0 in each alpha=anynumber in the plot:

ax110.scatter(xs1,ys1  , marker='o', color='gray'  , s=1.5,zorder=0,alpha=0.3)#, label=label_bg)

and 3) finally a rasterized=True when saving:

P.savefig(str(PLOTFILENAME)+'.eps', rasterized=True)

Note that this may not work as expected with the transparent keyword to savefig because an RGBA colour with alpha<1 on transparent background will be rendered the same as the RGB colour with alpha=1.

Coyle answered 19/2, 2016 at 0:51 Comment(0)
H
2

As mentioned above, the best and easiest choice (if you do not want to loose resolution) is to rasterized the figure

f = plt.figure()
f.set_rasterized(True)

ax = f.add_subplot(111)

ax.set_rasterized(True)
f.savefig('figure_name.eps',rasterized=True,dpi=300)

This way, you can manage the size by dpi option as well. In fact, you can also play with the zorder below you want to apply the rasterization:

ax.set_rasterization_zorder(0)

Note: It is important to keep f.set_rasterized(True) when you use plt.subplot and plt.subplot2grid functions. Otherwise, label and tick area will not appear in the .eps file

Hello answered 23/11, 2016 at 19:49 Comment(0)
P
1

My solution is to export the plot as .eps, load it up to Inkscape for example, then Ungroup the plot, select the object that I want to set the transparency and just edit the Opacity of the Fill in the "Fill and Stroke" tab.

You can save the file as .svg if you want to tweak it later, or export the image for a publication.

Plague answered 10/9, 2015 at 9:26 Comment(0)
P
1

If you are writing the academic paper in latex, I would recommend you export the .pdf file rather than .eps. The .pdf format supports transparency perfectly and has good compression efficiency, and most importantly, can be easily edited in Adobe Illustrator.

If you wanna further edit the graph (NOT EDITING DATA! I MEAN, FOR GOOD-LOOKING), you could open the exported graph, in Adobe Acrobat - Edit - Copy elements into Adobe Illustrator. The two software can handle everything perfectly.

I work happily with this method. Everything clear, editable and small-size. Hope can help.

Persuasive answered 16/3, 2021 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.