Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease file size of exported plots while keeping labels sharp

When exporting rather complicated plots (especially ListDensityPlot) as a PDF or EPS (for publication, for example), the resulting file size can be quite large. For example:

data = Flatten[Table[{f0, f, Exp[-(f - f0)^2/25^2]}, {f0, 500, 700, 5}, {f, 300, 
 900}], 1];
plot=ListDensityPlot[data,PlotRange->{Automatic,Automatic,{0,1}},InterpolationOrder->0]

Mathematica graphics

This example data set is on the order of the size I typically work with. When I export using Export["C:\\test.pdf", plot], it generates a PDF file 23.9MB in size. If I instead try Export["C:\\test1.pdf", Rasterize[plot]] it is far smaller, but the integrity and rescalability of the image naturally suffers.

This is complicated further if my actual figure is a combined plot, such as (Edit: f goes to 900)

plot2 = Show[plot, Plot[x, {x, 500, 900}, PlotStyle -> Thick]]

Mathematica graphics

(or with some usage of Epilog) where I'd love to have the background ListDensityPlot be rasterized, but keep the other markup and plots in ``vector'' form. Or at the very least, the frame labels be non-rasterized.

Is there any way to do this?

Or, to accomplish the same goal via some other clever method?


Update

I've checked out the related question, but that's gotta be way more complicated than it needs to be (essentially exporting then importing). I've been able to utilize some of the tricks in that question to extract the plot separately from the axes:

axes = Graphics[{}, Options[plot2]]

Mathematica graphics

plots = Graphics[plot2[[1]]]

Mathematica graphics

But, the plots term loses the AspectRatio and PlotRange, etc. plots can be hit with a Rasterize, but it needs dimensional fixing.

And then, how to combine them together?

like image 732
Eli Lansey Avatar asked Jan 03 '12 19:01

Eli Lansey


1 Answers

This is exactly the kind of problem for which I wrote the function linked here: http://pages.uoregon.edu/noeckel/computernotes/Mathematica/listContourDensityPlot.html

It's based on the same idea as in Heike's answer -- I just added some more features so that you can safely change the aspect ratio, opacity, and combine with other plots. See my comment in Heike's answer.

To try it with your data, do something like this:

plot = Show[
 listContourDensityPlot[data, 
  PlotRange -> {Automatic, Automatic, {0, 1}}, 
  InterpolationOrder -> 0, Contours -> None], 
 Graphics[Line[{{500, 500}, {700, 700}}]]]

There are a couple of similar functions linked from the parent page, too.

like image 196
Jens Avatar answered Sep 27 '22 23:09

Jens