2
$\begingroup$

I have coloured a parametric plot with using ColorFunction, which looks fine in Mathematica (I'm on M13 ):

ColourList = {Purple, Blue, Green, Orange, Red};
    
TestImage = 
    Show[
            {
                ParametricPlot[
                                {Exp[-t/3 ] Sin[2 \[Pi] \[Nu]0 t], (-1/3 Exp[-t/3 ]Cos[2 \[Pi] \[Nu]0 t] / (2 \[Pi] \[Nu]0))}, {t, 0, tf},
                                PlotStyle->Thick, ColorFunction->Function[{x, y}, Blend[ColourList, Sqrt[x^2 + y^2] / 1]], ColorFunctionScaling->False
                              ]
            },  
            FrameLabel->{"X", "Y"}, FrameTicksStyle->Directive[FontOpacity->0, FontSize->1/1000], Axes->False, ImageSize->Automatic->300, PlotRange->{All, All}
        ]

However, when I export it with

Export["TestImage.pdf", TestImage, ImageResolution->100]

The plot exports with very poor resolution:

enter image description here

If I remove the ColourFunction code, I get:

enter image description here

Here the image exports with the expected and correct resolution. Note how the image resolution affects not only the actual plot, but also affects the the plot label.

Does anyone know why this happens, and a work-around/fix? My guess would be that ColorFunction must have some sort of default resolution which is then being attributed to the entire plot, but I could not see any mention in the documentation.

$\endgroup$
1
  • $\begingroup$ Please include definitions for \[Nu]0 and tf. The code you have included is not evaluating. $\endgroup$
    – Syed
    Apr 29, 2022 at 22:31

2 Answers 2

4
$\begingroup$

I encountered the same problem a year or so ago. The problem lies with the graphic directives used here. Mathematica uses GraphicsComplex which allows for a line with multiple colors but there is no such/similar directive in the PDF file format. A single path is usually used for the lines generated by Plot or ParametricPlot and there is no support in the PDF file format (as far as I know) for multiple colors on one path. This is why Mathematica rasters the whole plot including the axes (which I find to be a pretty poor solution for situations like this: why not raster only elements which can not be vectorized and leave the rest but I digress).

The workaround I used for my plots was generating the path from individual segments and coloring each segment separately. Here a minimal working example with a simple curve

cfun=Function[{x,y,t},Hue[t/(2\[Pi])]];
p1=ParametricPlot[t{Cos[t],Sin[t]},{t,0,2\[Pi]},ColorFunction->cfun,ColorFunctionScaling->False]

which leads after export to PDF to a rasterized version of the plot:

problem

Manually creating the segments and overlaying them with a dummy (basically empty plot)

Table[{t Cos[t], t Sin[t], t}, {t, Subdivide[0, 2 \[Pi], 200]}] // N;
{CapForm["Round"], Thickness[0.005], cfun @@ Mean[#], 
    Line[#[[All, {1, 2}]]]} & /@ Partition[%, 2, 1];
p2=Show[{ListPlot[%%[[All, {1, 2}]], PlotStyle -> None], Graphics[%]}]

gives a vectorized result after exporting to PDF:

fix

CapForm["Round"] is a neat option to make the connection between segments look nice, especially when using a lower number of segments or when plotting more adventurous curves. One way to automate this fix is to replace the specific GraphicsComplex in use here with a list of segments using e.g.

p1/.GraphicsComplex[cords_,line_,VertexColors->colors_]:>Map[
  {CapForm["Round"],Thickness[0.005],Blend[{colors[[First@#]],colors[[Last@#]]},1/2],Line[{cords[[First@#]],cords[[Last@#]]}]}&,
  Partition[FirstCase[line,Line[___],{},Infinity]/.Line[x_List,y___]:>x,2,1]
]

which works fine for this simple example. When dealing with Graphics with multiple GraphicsComplex objects one might adjust the rule to apply it only to the relevant ones.

$\endgroup$
1
  • $\begingroup$ Nice diagnosis and workaround! $\endgroup$
    – user27119
    Apr 30, 2022 at 8:33
0
$\begingroup$

Just to make a further addition for anyone that comes across this, the accepted answer is an excellent work around -- especially if one wishes to export the plot explicitly as a pdf.

A slightly simpler workaround is to export the image as a png and increase the image resolution as required. I was only able to come by this thanks to the accepted answer's explanations that the issue is related to the pdf format:

...there is no support in the PDF file format (as far as I know) for multiple colors on one path...

$\endgroup$

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.