Whenever I output figures for academic research papers, I try as much as possible to prevent rasterization, which is one of my personal quirks, I guess. I have succeeded so far using Mathematica, but I sometimes bump into unexpected rasterization.

Consider the following simple example:

Plot[Exp[-x^2], {x, -5, 5}, ColorFunction -> "Rainbow"]

The output file is rasterized, and as far as I can tell, it is because of the color gradient. My knowledge of vector graphics is limited, and I understand that trying to output a vectorial image from a DensityPlot is borderline insane (and looking on stackexchange there are many clever ways around this by rasterizing only the heatmap and not any of the text or curves). However, it doesn't seem crazy to have a simple plain vector with a color gradient, Illustrator or Inkscape can do this very easily and the output file is lightweight.

I have tried many different ways, but couldn't prevent rasterization in this simple example.

Is there an easy way out of this, or one would need to overlay a rasterized curve on top of a blank plot ?

Many thanks.

  • 1
    $\begingroup$ Very strange. Even ListPlot[Table[Exp[-x^2], {x, -5, 5, .1}], ColorFunction -> "Rainbow"], which should be very simple, is rasterized. $\endgroup$
    – MelaGo
    May 18, 2021 at 1:46
  • $\begingroup$ And it is also interesting that ContourPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, PlotRange -> All] is NOT rasterized, whereas it is a much more complicated figure. I'm guessing the step between contours is discrete so there is no "continuous" variation of heat map. This means however that it isn't the use of ColorFunction that results in rasterization, but rather on how it is used.... $\endgroup$ May 18, 2021 at 3:12

1 Answer 1


It's because (I think) ColorFunction is implemented via VertexColors in the Line object. Even with monochromatic ColorFunction -> (Red &), the PDF is rasterized. To avoid this, the graph can be discretized into separate line segments, but the line caps don't join up exactly. With such an approach, I don't think there's a way to avoid this issue.

Plot[Exp[-x^2], {x, -5, 5}, MeshFunctions -> {#2 &}, Mesh -> 50, 
 MeshStyle -> Opacity[0], 
 MeshShading -> Array[ColorData@"Rainbow", 51, {0., 1.}]]
Export[FileNameJoin[{$TemporaryDirectory, "test.pdf"}], %]
  • $\begingroup$ I suspected that a monochromatic ColorFunction would result in a rasterized plot, I seem to remember having this problem at some point. Too bad then, but I guess it's good to know that there isn't really any easy way to have any vector output when using ColorFunction. $\endgroup$ May 18, 2021 at 3:07
  • $\begingroup$ Note that, starting to mess around with ColorFunction, sometimes it doesn't result in rasterization, cf. my previous comment concerning ContourPlot... Sometimes, Mathematica works in mysterious ways... $\endgroup$ May 18, 2021 at 3:15
  • $\begingroup$ @ValérianThiel ContourPlot, even with ColorFunction, resembles Plot + MeshShading (each polygon, resp. line, gets a single color). DensityPlot is a closer analogy to Plot + ColorFunction (each polygon, resp. line, get colored by VertexColors). As I meant to imply, it's VertexColors that I think triggers graphics to be rasterized. Whether a *Plot function implements ColorFunction with VertexColors I considered only for Plot itself. I think the look of ContourPlot suggests it does not use VertexColors, but DensityPlot looks like it does (due to the gradient). $\endgroup$
    – Michael E2
    May 18, 2021 at 4:16
  • $\begingroup$ This explains a lot and makes a lot sense, thank you. I've given up and settled for rasterizing only the color gradient lines and overlayed the rest. $\endgroup$ May 18, 2021 at 8:04

Your Answer

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

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