5
$\begingroup$

When I generate multiple scatter plots with a large number of points and display them as output, the notebook becomes very sluggish (I assume that this is because each plot has a huge number of points and the system has trouble with the graphics memory or something similar).

I would like to know if it is possible to set things up in such a way that the displayed figures are some sort of png but not vector graphics.

To be clearer: I wish to automatically replace the standard plot output with something like its screenshot. This is just about what I see in the notebook and has nothing to do with the nature of the Graphics object.

I looked around but couldn't find how to do it or whether it is possible.

Thanks! Enrico

$\endgroup$
1
  • 1
    $\begingroup$ An alternative to a scatter plot for some cases is SmoothDensityHistogram $\endgroup$
    – mikado
    Commented Oct 23, 2018 at 20:21

4 Answers 4

3
$\begingroup$

I wish to automatically replace the standard plot output with something like its screenshot.

Take a look at $Post:

$Post = Replace[g : (_Graphics | _Graphics3D) :> Image[g]]

will convert any returned graphics or 3D graphics into an image.

Note this only works if the output is exactly a Graphics, and doesn't look inside. So it would rasterize Plot[x, {x,-2,2}] but not {Plot[x,{x,-2,2}]. To catch the latter, change the Replace above to a ReplaceAll

$\endgroup$
1
  • $\begingroup$ You could add Legended as well. Personally I use something like im = Image[Rasterize[#, "Image", ImageResolution -> 2*72], Magnification -> 1/2] &; to get a nice retina rendering. $\endgroup$
    – Szabolcs
    Commented Nov 22, 2018 at 22:19
3
$\begingroup$

You can set the $DisplayFunction (default setting for the option DisplayFunction in graphics functions) to Rasterize:

$DisplayFunction = Rasterize[#, RasterSize -> 100] &;

{Plot[Sin[x], {x, -2, 2}], 
 Histogram3D[RandomVariate[NormalDistribution[0, 1], {500, 2}]], 
 BarChart[RandomInteger[10, {5, 3}]]}

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ (+1) It sometimes work with Legended too, what is pleasant (but undocumented). $\endgroup$ Commented Nov 23, 2018 at 13:24
0
$\begingroup$

I think you can do what you ask for with the following:

data = RandomVariate[BinormalDistribution[0.5], 1000000];
Image[ListPlot[data]]
$\endgroup$
0
$\begingroup$

Rasterize is an option. For example:

Rasterize[Plot[Sin[x], {x, -2, 2}], RasterSize -> 50]

gives a very coarse plot while

Rasterize[Plot[Sin[x], {x, -2, 2}], RasterSize -> 500]

gives better resolution.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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