12
votes

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?

4
Thanks for that link. Looks like I might be able to work with that. What's the appropriate way to "cancel" a question? - Eli Lansey
We can close a question if there are five 'close' votes, but this is usually done only when the question is very similar to an earlier question. The closed question then serves as a pointer to the earlier question. I have to compare both questions more closely to see whether they are duplicates. It's perhaps best to don't do anything right now. - Sjoerd C. de Vries
OK, it's not exactly the same. But I may be able to use some of the tricks there. - Eli Lansey
As an active member of the Mathematica tag, have you considered committing to the Area 51 Mathematica proposal? We could use your help. - rcollyer

4 Answers

6
votes

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.

5
votes

If you're dealing with 2D plots, you could combine a rasterized plot with vectorized axes by using Inset. For example

plot2 = ListDensityPlot[data, 
   PlotRange -> {Automatic, Automatic, {0, 1}}, 
   InterpolationOrder -> 0, Axes -> False, Frame -> False, 
   PlotRangePadding -> 0];

plotRange = PlotRange /. AbsoluteOptions[plot2, PlotRange];

plot = Graphics[{
  Inset[Image[plot2], plotRange[[All, 1]], {Left, Bottom}, Scaled[{.96, .96}]],
  Line[{{500, 500}, {700, 700}}]}, 
 Frame -> True, AspectRatio -> 1, 
 PlotRange -> plotRange, PlotRangePadding -> Scaled[.02]]

Export["test.pdf", plot]

produces a .pdf of about 400 KB. The frame, tick marks, and black line are still vectorized, so they stay sharp when zooming in:

detail of image

0
votes

If you are exporting as PDF, EPs or WMF, then the text should remain as vectors even if you have a rasterized component to the graphics.

I think the trick is to set the number of plot points to some low number in the ListDensityPlot command and then export as PDF as normal.

0
votes

How about just plotting the function rather than making a list?

plot=DensityPlot[Exp[-(f - f0)^2/25^2], {f0, 500, 700}, {f, 300, 900}, 
 Epilog -> {Thick, Line[{{500, 500}, {700, 700}}]}, PlotPoints -> 50]

Export["test.pdf", plot]

file size 1.1MB