3
$\begingroup$

I would like to produce a output PDF file of a log-linear plot obtain by composing the plot of an analytical function and a scatter plot of some random data. Since the random points are of the order of $10^4$ or even more, a full-vector PDF is very heavy and PDF viewers get very slow.

Here is a simplified example of my plot:

list = With[{n = 500}, 
    MapThread[{10^#1, #2*(1 - Exp[-10^#1])} &, 
        {RandomReal[{-3, 2}, {500}], RandomReal[{0, 1}, {500}]}]];
Show[ListLogLinearPlot[list, PlotStyle -> LightGray], 
     LogLinearPlot[1 - Exp[-x], {x, 0.001, 100}]]

Example plot

To overcome this problem I would like to have the random points rasterized, while keeping the axes and the analytical function in vector format for readability.

I tried by rasterizing the output of ListLogLinearPlot with Axes -> None with Inset and Overlay, but I'm unable to find a way to superimpose it to the other plot and make the two figures coincide. I've also tried to modify the code in this answer to no avail.

How can I achieve the desired result? Ideally, the method should be robust to changes in the PlotRange, i.e. it shouldn't require manual fine-tuning of position and size.

$\endgroup$
  • $\begingroup$ How important is it that your PDF show 10^4 dots? Would something like Take[list,{1,-1,10}] in your ListLogLinearPlot function produce an acceptable output? $\endgroup$ – bobthechemist Feb 26 '15 at 17:40
  • $\begingroup$ @bobthechemist it could be a workaround, but I would prefer to keep all the points. $\endgroup$ – Pincopallino Feb 26 '15 at 17:45
2
$\begingroup$

Something like that?

With[{
  opt = Sequence[PlotRange -> {{.001, 100}, {0, 1}}, ImagePadding -> 45,
                 ImageSize -> 800, Axes -> False, Frame -> True]
  },


 LogLinearPlot[1 - Exp[-x], {x, 0.001, 100}, opt, 
               PlotStyle -> {Green, Thick}, 
               Prolog -> Inset @ Rasterize[
                  ListLogLinearPlot[list, Frame -> False, Axes -> False, opt], 
                  RasterSize -> 1000]]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Yes I would like something like this. However, it seems like the values provided for ImagePadding and ImageSize are manually tuned. Do you think that there is a way to obtain this output programmatically? I.e., I specify the image size and all other parameters are found by Mathematica? $\endgroup$ – Pincopallino Feb 26 '15 at 19:02
  • $\begingroup$ @Pincopallino they are not manually tuned. they are arbitratry but have to be the same for each plot. So you can put here ImagePadding -> RandomInteger[{1,100}] if you want ;) $\endgroup$ – Kuba Feb 26 '15 at 19:05
  • $\begingroup$ You are right. I replaced the Frame with the axes in your code and the raster was misplaced. If using the frame everything looks great even if I change the size and padding. $\endgroup$ – Pincopallino Feb 26 '15 at 19:18
  • $\begingroup$ @Pincopallino now it should be ok. I added Axes -> False to rasterized plot. $\endgroup$ – Kuba Feb 26 '15 at 19:26
  • $\begingroup$ Yes, the key is that you used Frame instead of Axes. In my previous attempts I kept using Axes and always had the raster misplaced. By setting the frame to None in top and right positions, however, I can emulate the appearance of axes. Thank you very much! $\endgroup$ – Pincopallino Feb 27 '15 at 18:35
1
$\begingroup$

Here's an attempt using Overlay instead of Show:

vp = LogLinearPlot[1 - Exp[-x], {x, 0.001, 100}, 
  PlotRange -> {{0, 100}, {0, 1}}, Background -> Directive@Opacity[0]]

rp = Rasterize[
  ListLogLinearPlot[list, PlotStyle -> LightGray, AxesStyle -> Opacity[0], 
   PlotRange -> {{0, 100}, {0, 1}}], Background -> None]

Overlay[{rp, vp}]

Mathematica graphics

One possibility is to Rasterize both plots:

g = Rasterize[
  ListLogLinearPlot[list, PlotStyle -> LightGray, 
   PlotRange -> {{0, 100}, {0, 1}}], Background -> None]
h = Rasterize[
  LogLinearPlot[1 - Exp[-x], {x, 0.001, 100}, 
   PlotRange -> {{0, 100}, {0, 1}}], Background -> None]
Show[g, h]

Mathematica graphics

An important note here is that both plots are given the same explicit PlotRange and that both Rasterize functions contain the same Options.

$\endgroup$
  • $\begingroup$ As written in the question, I would like to keep the axes and the line plot in a vector format. $\endgroup$ – Pincopallino Feb 26 '15 at 18:00
  • $\begingroup$ @pincapallino True, and I don't suggest that my answer will be the best one; however it's not clear what you mean by "desired output". Small file size? Visualizing 10000 points on a plot? Rubustness? I interpreted your question as wanting a method to produce an image with 10^4 rasterized points that doesn't result in significant slowdown of a PDF viewer and avoids manual tuning of parameters. Can you edit your question to include why some vector formatting is necessary? $\endgroup$ – bobthechemist Feb 26 '15 at 18:26
  • $\begingroup$ when producing plots for printing or publishing vector format is preferred for readability and print quality. As including 10^4 vector points gives PDF files of several MB and causes readers to slow down when drawing all the points. My request is thus to keep the simple parts of the plot (axes and the line) in vector format and superimpose them to the rasterized points. Achieving a balance between file size and complexity and overall quality. I would like something similar to what is achieved in the question that I linked above. $\endgroup$ – Pincopallino Feb 26 '15 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.