12

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?

5
  • 1
    Related question: stackoverflow.com/q/6301676/615464 Commented Jan 3, 2012 at 19:53
  • Thanks for that link. Looks like I might be able to work with that. What's the appropriate way to "cancel" a question? Commented Jan 3, 2012 at 20:00
  • 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. Commented Jan 3, 2012 at 20:05
  • OK, it's not exactly the same. But I may be able to use some of the tricks there. Commented Jan 3, 2012 at 20:34
  • As an active member of the Mathematica tag, have you considered committing to the Area 51 Mathematica proposal? We could use your help. Commented Jan 8, 2012 at 15:22

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Wow! This is pretty much exactly what I'm looking for. I can basically convert all my ListDensityPlots to listContourDensityPlot with the switch Contours -> None. Thanks so much!
I am curious regarding the usage of AbsoluteOptions[p] vs Options[p], the latter of which works better for a wacky FrameTicks specification.
I think I could use FullOptions, but not just Options, because I wanted to avoid getting symbolic vales like Automatic, All etc. They wouldn't work in the subsequent calculations. Maybe your problem with ticks can be solved by specifying PlotRangePadding (which I set to zero by default because I like it better). If you want to restore the usual behavior of adding 4% padding, just add the following just before the options in the Show statement, immediately before Evaluate@Apply[Sequence, frameOptions]: PlotRangePadding -> If[FreeQ[{opts}, PlotRange], .04 {-1, 1}.rangeCoords, 0]
AbsoluteOptions has scores of faults currently that tech support acknowledge as bugs.
Yeah it is something that needs a total revamp for V9.
|
5

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

4 Comments

I personally also favor this general approach. One just has to be careful with the Scaled coordinates. Try this with your plot to see what I mean: Show[plot, Graphics[{EdgeForm[Red], FaceForm[], Rectangle[{0, 0}, {1000, 1000}]}], PlotRange -> All]
@noeckel I agree that using Inset in this way is rather fragile; even changing the aspect ratio breaks it. Ideally I would like to do something like plot = Graphics[{Texture[plot2], Polygon[Tuples[plotRange][[{1, 2, 4, 3}]], VertexTextureCoordinates -> Tuples[{0, 1}, 2][[{1, 2, 4, 3}]]]}, Frame -> True] but for some reason, Mathematica refuses to export the resulting plot to pdf (at least in my version of Mathematica).
I like this approach, but I think @noeckel wrote an excellent function.
@heike I tried the same thing with the same result. My recollection is that I attributed it to a bug in vertex color PDF export, but I didn't pursue it further.
0

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.

2 Comments

My hope is to generate a rasterized plot, with non-rasterized text, and export that. Even adjusting MaxPlotPoints, that might not help for some more complicated graphs which require lots of points.
Also, using MaxPlotPoints->2 which is the smallest possible option, doesn't change the exported file size.
0

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

4 Comments

This was a sample data set. In practice I'm working with the output from many computer simulations.
And the function plotted is far more complicated than a straight line.
@Eli Lansey can you update with information about the real life data, is it 3D or do you transform it to 3D etc. etc. Maybe provide a sample. So you want to overlay the plot of function onto the density plot of your list? There is Show, Overlay and Inset (as per Heike's example).
The data in the problem is an artificially constructed, simple, but decent representative of the type of, format of, and size of data I typically work with. I then compare the data set with theoretical models, which I overlay on the graph. My goal is to get a smaller exported file for inclusion in publications.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.