EPS opacity

When saving graphics in Mathematica, can I save numbers with opacity in EPS format? For instance,

Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis] 

gives the following drawing, which saves neatly in any format other than EPS.

Transparency in mma

If I try to save EPS (in Mathematica 7), the result will look like

EPS from Mma7

In Mathematica 8, it looks like

EPS from Mma8

Does anyone know how to get opacity in EPS graphics (or if possible)? The option "use rasterization for transparency" does not look as impressive as the true EPS value when scaling.

+4
source share
3 answers

I usually rasterize my graphics in this situation. Try

 Rasterize[Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis], RasterSize -> 600, ImageSize -> 400] 

Of course, the result will not be scalable and may take up more memory. You can partially solve the scalability problem by installing RasterSize more than ImageSize, as I did here.

+3
source

OK, so EPS cannot support true transparency / opacity, but that does not mean that Mathematica 7 apologizes for such poor performance. As my Mathematica 8 testifies, you can make it better.

The problem with the release of Mathematica 7 is that it uses the same color for the fill as for the curves, when you really need a lighter color. EPS is a simple text format, so it's pretty easy to write a quick hack. Here's a quick tute on PS graphics.

In the PS-graphic, you define the path that you say, whether you want to stroke it (lines) or fill (areas) - or other things that we do not need to worry about. Colors are set and remain there until they are reset. So I just import the EPS created by Mma7 and find all the filled paths. For each filled path, you find the previous color and reset the color just above the fill command to be something easier.

So here is an example (I did not bother to pack it into one script / module). All products from Mathematica 7.0.1

 p = Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis] 

original image

Export to EPS file using Export["BesselJs7.eps", p] . This creates terrible graphics like original eps

Ok now fix

 pList = Import["BesselJs7.eps", "List"]; (* Import image as a list of strings *) FList = Flatten@Position [pList, "F"]; (* find all fill commands *) 

Notice that the EPS file has the line /F { fill} bind def , which defines the label F In addition, you can check that pList[[FList - 1]] lists "closepath" s.

 FColorList = {}; (* get list of colors associated with fills *) Catch[Do[ i = 0; rgb = True; newpath = True; While[rgb || newpath, (*Print[{f,i,fi,rgb,newpath,pList[[fi]]}];*) If[rgb && StringMatchQ[pList[[f - i]], __ ~~ "r"], rgb = False; AppendTo[FColorList, pList[[f - i]]]]; If[newpath && StringMatchQ[pList[[f - i]], "newpath" ~~ __], newpath = False; np[f] = f - i]; If[f - i == 1, Throw[{f, rgb, newpath}]]; i++], {f, FList}]] 

Now crack new colors - all I do is add .5 to each rgb value. This can be done better:

 FColorListNew = Table[ Most@ToExpression @StringSplit[f] + .5, {f, FColorList}] /. _?(# > 1 &) -> 1.; FColorListNew = Table[StringJoin[{Riffle[ToString /@ f, " "], " r"}], {f, FColorListNew}]; 

Finally, insert the new colors and write them back:

 Do[pList = Insert[pList, FColorListNew[[i]], np[FList[[i]]] + i], {i, Length[FList]}] Export["BesselJsFixed.eps", pList, "List"] 

fixed EPS

Some things, such as finding newpath locations, are not needed, and all of this can probably be matched. But I spent enough time on this!

+3
source

Customize Mark's answer to export PNG with transparency, use:

 Export[ "testfile.png", Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis], ImageSize -> 600, Background -> None ] 
+1
source

Source: https://habr.com/ru/post/1345043/


All Articles