19
$\begingroup$

When exporting a dashed plot with a color gradient to a vector graphics format such as EPS or PDF, the dashing is partly ruined. For example, this command:

Plot[x^2, {x, -2, 2}, PlotStyle -> Directive[Dashed, Thick], ColorFunction -> Hue]

Creates the following plot:

A dashed plot with a color gradient exported to png

However, when exporting the plot to EPS or PDF, I get this plot (notice the ruined dashing, especially near the vertex of the parabola):

A dashed plot with a color gradient exported to eps

  • Am I doing something wrong, or is this a bug?
  • If it is a bug, is there a workaround?

I am using Mathematica version 8.0.4.0 (edit: on Win7 64bit)

Edit: I got an email from wolfram support, which I understand confirms this is a bug. Until they fix it, any workarounds which properly export the plot as vector graphics are welcome.

$\endgroup$
5
  • $\begingroup$ On what platform? $\endgroup$
    – ragfield
    May 22, 2012 at 14:49
  • 1
    $\begingroup$ Reproduced on 8.0 for Mac OS X x86 (64-bit) (October 5, 2011) $\endgroup$
    – rm -rf
    May 22, 2012 at 14:53
  • 1
    $\begingroup$ Reproduced on WinXP/8.0.4 as well. $\endgroup$
    – Szabolcs
    May 22, 2012 at 15:34
  • 1
    $\begingroup$ Also reproducible under Win7 64bit/8.0.4, both with Export[...] and withe the Save Graphics as... menu option. $\endgroup$ May 22, 2012 at 15:39
  • $\begingroup$ The answer given by @Peter below is based on a post by me on MathGroup. I also gave it as an answer on this web site, see the bottom of my answer. $\endgroup$
    – Jens
    May 22, 2012 at 15:54

2 Answers 2

7
$\begingroup$

The VertexColors that get introduced when a ColorFunction is specified are not rendered smoothly in the exported PDF - no matter what other options (such as Dashed) you specify. So we really have two problems here. Bad color gradients along the line, and incorrect computation of the dash spacing.

If you don't care too much about the bad color gradients and just want a plot with an acceptable dashing, you could do something like this:

Show[#,
   Graphics[{Thickness[.01], White, Dashing[{.02, .04}], 
     Cases[Normal@#[[1]], Line[_], Infinity]}]] &@
 Plot[x^2, {x, -2, 2}, PlotStyle -> Thickness[.008], 
  ColorFunction -> Hue]

Here I have taken your original Plot and initially removed the dashing. Before showing this plot, I extract from it the Line that was produced, re-draw it a second time with dashing and slightly larger thickness at the background color (White).

Finally, I place the dashed white line on top of the continuous colored line to get the appearance of a dashed colored line. For this to look correct in the exported PDF, you have to play with the Dashing and Thickness manually.

The dashing in the export will look different from the notebook. In particular, the second number in Dashing[{0.02,0.04}] must always be bigger than the first, or else you don't see anything!

Here is what the exported PDF looks like with the above command:

PDF

If you do care about the bad color gradients in addition to the dashing, then I would suggest going with bitmap export right now because it would take a bitmap-based hack to get smooth color gradients in the PDF anyway.

$\endgroup$
1
  • $\begingroup$ Nice! I guess that's the best that can be done until Wolfram fixes the bug... $\endgroup$
    – Joe
    May 31, 2012 at 5:47
6
$\begingroup$

The wrong dashing goes away if you use the following rule, which I found in this MathGroup post by Jens, as a hint to downsize the pdf-size and to eliminate the spurious edges coming up with saved DensityPlots, ParametricPlot3D etc. The setting is:

rasterProlog={{EdgeForm[], Texture[{{0, 0, 0, 0}}], 
  Polygon[{{0, 0}, {1, 0}, {1, 1}}, 
   VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}}]}}

All one has to do to write: pl = Plot[..., Prolog -> rasterProlog] and then Export.

Export[filename, Show[pl, Prolog->rasterProlog] works as well, if you don't want to include the Prolog in Plot, DensityPlot, etc.

$\endgroup$
1
  • 4
    $\begingroup$ Thank you for your answer. However, your solution rasterizes the plot, which defeats the purpose of using a vector graphics format such as EPS or PDF. I want a vector graphics format since it allows enlarging the plot as much as I want without loss of quality, and it allows editing individual elements in the saved plot as discrete objects. If I wanted a rasterized plot I could have just used a raster format such as PNG, since when exporting to a raster format the dashing isn't ruined in the first place. $\endgroup$
    – Joe
    May 23, 2012 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.