5
$\begingroup$

My first post on here, hope it's clear:

When using ListDensityPlot, I find that the axes and tick labels are not vector graphics (i.e. they're blurry when you zoom in), when exported as a PDF. I've tried Export, Save Graphic / Selection As... -> PDF, File -> Print Selection... -> PDF, and none of them work on their own. Is there a simple way to do this?

I've posted my "solution" below as an answer to this question - it might be helpful to others who have the same problem in the meantime, but maybe there is a better solution out there.

Extra points:

  • Exporting as an SVG produces a terribly blurry image (no idea why). I'm really after a vector graphic, not a high-definition JPEG or PNG.

  • I've tried 'save graphic as -> options -> highest quality vector resolution' (and all the other combinations) and it still produces a burry ~214kB image.

  • plotting using Plot[...] and similar functions for line plots does not have this problem at all (only the known 'save as' bold font problem).

  • I'm using Mathematica 11.3.0.0 on Max OS X x86.

  • I'm pretty sure previous recent versions didn't have this problem.

Minimum working example:

xmin = 0;
xmax = 10;
xstep = 0.5;
ymin = 0;
ymax = 20;
ystep = 0.5;

data = Table[
   x^2 + y^2, {y, ymin, ymax, ystep}, {x, xmin, xmax, xstep}];

plot = 
  ListDensityPlot[data , DataRange -> {{xmin, xmax}, {ymin, ymax}}, 
   ColorFunction -> "Rainbow", 
   LabelStyle -> {20, Black, FontFamily -> "Helvetica"}]

Which produces this (exported as a PNG to post on here): png export of plot

And this is a PNG screenshot of an zoomed-in, exported PDF version of the image, to show how blurry it is:

screenshot of zoomed pdf

Thanks in advance!

$\endgroup$

2 Answers 2

2
$\begingroup$

My solution is a bit clunky, it involves:

1) Making a list density plot without the axes,

2) Making an empty set of axes to match the one above. You ensure they're matched by including:

PlotRangePadding -> Scaled[0.02], 
AspectRatio -> 1, 
ImagePadding -> {{40, 40}, {40, 40}}, 
ImageSize -> 400" 

in both the ListDensityPlot and the empty axes plot (with your choice of values, as long as they're the same in both plots).

3) overlaying both images.

Full solution below (with some sanity-checking by comparing to a ListDensityPlot with axes):

xmin = 0;
xmax = 10;
xstep = 0.5;
ymin = 0;
ymax = 20;
ystep = 0.5;

data = Table[
   x^2 + y^2, {y, ymin, ymax, ystep}, {x, xmin, xmax, xstep}];

(* 1: plot the graph of data with no axis:*)
plotnoaxis = 
  ListDensityPlot[data , DataRange -> {{xmin, xmax}, {ymin, ymax}}, 
   PlotRangePadding -> Scaled[0.02], ColorFunction -> "Rainbow", 
   AspectRatio -> 1, Axes -> False, Frame -> False, 
   ImagePadding -> {{40, 40}, {40, 40}}, ImageSize -> 400];

(* 2: plot of the above with the axis.
If you try to export this as a PDF, the frame/labels will not be
vector graphics: *)

plotwithaxis = 
  ListDensityPlot[data , DataRange -> {{xmin, xmax}, {ymin, ymax}}, 
   PlotRangePadding -> Scaled[0.02], ColorFunction -> "Rainbow", 
   AspectRatio -> 1, ImagePadding -> {{40, 40}, {40, 40}}, 
   ImageSize -> 400, 
   LabelStyle -> {20, Black, FontFamily -> "Helvetica"}];


(* 3: now plot an empty axis - this should have the same positioning as above *)

axis = Graphics[ Frame -> True, 
   PlotRange -> {{xmin, xmax}, {ymin, ymax}}, AspectRatio -> 1, 
   PlotRangePadding -> Scaled[0.02], 
   ImagePadding -> {{40, 40}, {40, 40}}, ImageSize -> 400, 
   LabelStyle -> {20, Black, FontFamily -> "Helvetica"}];


(* 4: align all plots above and then overlay to confirm they're
positioned ok: *)

Overlay[{plotnoaxis, plotwithaxis, axis}]

All plots from 1-4 are plotted here, via File-> Print Selection -> PDF, arranging & labelling them, and saving as a PNG to upload on here:

enter image description here

You might notice that the overlay on plot 4 is not perfect... this seems to be due to the File -> Print Selection -> PDF option, because when you do 'Save Selection As -> PDF' they overlay perfectly. However, using 'Save...' makes the tick labels bold when you do it this way (argh). The two export methods are compared here (zoom in of the two PDF outputs shown below):

overlaid graphs, save or print

So the final result (which may not be perfectly positioned because I'm using 'File -> Print Selection... -> PDF' as above) can be obtained by overlaying the graph without the axes to the graphic with axes only. Note the result appears blurry on here because it's uploaded as a PNG:


(* now overlay the blank axis and the axis-less plot, this produces a
vector graphic when exported, use 'print selection... -> pdf' to
ensure font isn't bold *)
Overlay[{axis, plotnoaxis}]

png version of the exported pdf

Notes:

  • using 'Graphics[{Inset[Image[plotnoaxis]]}...]' as suggested in here still produces a non-vector image for me when exported to pdf.

  • Using 'Show[...]' for the graphics doesn't work for me, only Overlay seems to produce the correct result.

  • The density plot itself doesn't seem to be vectorised this way - its edges are still blurry, though since this part of the image is blurry anyway, it's not so noticeable.

$\endgroup$
0
$\begingroup$

I can reproduce the behaviour with Mathematica 11.3 on Windows. The following solution works for me:

  • Right-click the graphic, Save Graphic As... -> Click the Options button -> Change setting Graphics Containing Smooth Shading to Use Highest Quality Vector Representation

The resulting pdf file is now 1.7 MB in size (was 207 KB before) and seems to be a clear vector representation of the graphics.

$\endgroup$
1
  • $\begingroup$ Thanks for trying this - I have also tried those settings, including the 'highest quality' or 'most compatible' vector representations, combined with say 2000 dpi for the 'rasterisation resolution' but nothing works for me, all files are 214kB in size. Maybe it's a bug with Macs? $\endgroup$
    – njw
    Apr 25, 2019 at 8:49

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.