3
$\begingroup$

I would like to extract some pictures from a Manipulate that uses a slider, but when I do it from the pop-up menu, I get all the frame and slider with it. How can we export the picture (to PNG, say), for a given slider value (say $k = 0.5$, for the example below), without the Manipulate frame, slider, and even without the axes and ticks?

And probably trickier: how can we extract the graphics with an higher resolution than what the Manipulate shows? (say 1024 x 1024 pixels in PNG format, instead of the lower resolution of 600 below).

Here's a semi-cool MWE to play with:

Manipulate[
 ContourPlot[
  Cos[x + y] Sin[k x - 3 y],
  {x, -2, 2},
  {y, -2, 2},
  PlotPoints -> 20,
  PlotRange -> {{-2, 2}, {-2, 2}},
  Axes -> True,
  AxesOrigin -> {0, 0},
  AxesStyle -> Directive[GrayLevel[0.5]],
  AxesLabel -> {
    Style["X",  Bold, 14],
    Style["Y",  Bold, 14]
    },
  ColorFunction -> "Rainbow",
  ContourStyle -> Black,
  Contours -> Automatic,
  ImageSize -> 600
  ],
 {
  k, -5, 5, 0.1,
  ImageSize -> Large,
  Appearance -> {"Labeled", "Closed"},
  AppearanceElements -> {"InputField", "Slider"}
  },
 ControlPlacement -> Bottom,
 FrameMargins -> None,
 FrameLabel ->  {None, None, 
   Style["Some Funny Title", Bold, 14, FontFamily -> "Helvetica"]}
 ]
$\endgroup$
7
  • $\begingroup$ I click on the graphics and Edit > Copy. You can also click on the circled + button in the upper-right corner of the Manipulate frame and 'Paste Snapshot". For output, use Export on the copied graphics. $\endgroup$ – Michael E2 Dec 8 '20 at 20:00
  • $\begingroup$ @MichaelE2, the problem with this method is you can't choose the format (PNG say) and resolution. Or maybe there is but I don't know it. $\endgroup$ – Cham Dec 8 '20 at 20:06
  • $\begingroup$ @MarcoB, I understand for the resolution. I was extracting the picture by hand, by re-running the Manipulate code with a few modifications (remove axes, etc), but it's clumsy. $\endgroup$ – Cham Dec 8 '20 at 20:08
  • $\begingroup$ One of the ways I use quite often is to just go Manipulate[plt = ContourPlot[...], ...]. Then, when you change your sliders, the new image is stored to plt. Then, Export["path/plot.png", plt, ImageSize -> {1024, 1024}]. The resolution in your code isn't 600, that's just a width (well, I suppose it might end up being the resolution if there are no other options given on export). I usually choose a target resolution when exporting and ignore the size like Export["path/plot.png", plt, ImageResolution -> 300]. $\endgroup$ – MassDefect Dec 8 '20 at 20:08
  • 2
    $\begingroup$ You'll want to set TrackedSymbols in @MassDefect 's solution, or you'll recompute the plot. $\endgroup$ – Michael E2 Dec 8 '20 at 20:16
7
$\begingroup$
Manipulate[
  plt = ContourPlot[
    Cos[x + y] Sin[k x - 3 y], 
    {x, -2, 2}, {y, -2, 2}, 
    PlotPoints -> 20, 
    PlotRange -> {{-2, 2}, {-2, 2}}, 
    Axes -> True, 
    AxesOrigin -> {0, 0}, 
    AxesStyle -> Directive[GrayLevel[0.5]], 
    AxesLabel -> {Style["X", Bold, 14], Style["Y", Bold, 14]}, 
    ColorFunction -> "Rainbow", 
    ContourStyle -> Black, 
    Contours -> Automatic, ImageSize -> 600
  ], 
  {k, -5, 5, 0.1, ImageSize -> Large, Appearance -> {"Labeled", "Closed"}, AppearanceElements -> {"InputField", "Slider"}}, 
  ControlPlacement -> Bottom, 
  FrameMargins -> None, 
  FrameLabel -> {None, None, Style["Some Funny Title", Bold, 14, FontFamily -> "Helvetica"]}
]

Export["~/Desktop/test.png", plt, ImageSize -> {1024, 1024}]

            (* or *)

Export["~/Desktop/test.png", plt, ImageResolution -> 300]

            (* EDIT *)

Export["~/Desktop/test.png", plt[[1]]//Graphics, ImageSize -> {1024, 1024}]

This saves the frame generated by the slider setting to a variable called plt. Every time the slider changes, plt is updated. Then, I typically use the Export command so that I can more easily control the various parameters. A bit of an aside: one export parameter that I quite like is Background -> None. Not really relevant here, but it allows you to export PNG images with no background. Looks really slick when adding to a poster or presentation if the graphics have sufficient contrast with the background.

To get just the graphics form the ContourPlot I used @andre314's method of taking the first element of plt and turning it into a graphic, namely plt[[1]]//Graphics.

The result of the above code with ImageSize -> {1024, 1024} is:

Exported contour plot.

The result of the final export statement from above is:

Exported contour plot with no ticks.

$\endgroup$
5
  • $\begingroup$ Wooaa! This is cool! $\endgroup$ – Cham Dec 8 '20 at 20:22
  • $\begingroup$ @Cham Although I just realized that you asked to export with no ticks or axes labels either. You can actually just use the last step shown by Andre314 above. I'll add that to my answer (with attribution). $\endgroup$ – MassDefect Dec 8 '20 at 20:26
  • $\begingroup$ It works, and it's neat!! This is awesome! Apparently, it doesn't work of Plot3D, though. $\endgroup$ – Cham Dec 8 '20 at 20:38
  • 1
    $\begingroup$ @Cham Plot3D gives 3d graphics. We can fix it by using Graphics3D[plt[[1]], Boxed -> False] in place of plt[[1]]//Graphics. $\endgroup$ – MassDefect Dec 8 '20 at 22:27
  • $\begingroup$ Yep, it fixes this issue. Thanks a lot! $\endgroup$ – Cham Dec 8 '20 at 22:35
7
$\begingroup$

EDIT

I have found something simplier than my first answer:

Apply Setting to the Manipulate to obtain a normal graphic.

Then to remove all the options of this graphic, one way is to take the first part and reapply Graphic

enter image description here END EDIT

OLD ANSWER

You can use the Snapshot function in the Manipulate.

Then remplace DynamicModule by With (this may not be necessary),
then the evaluation of this gives a normal Graphics. To remove all the options of this graphic, one way is to take the first part and reapply Graphic

enter image description here

$\endgroup$
1
  • $\begingroup$ I've been trying to remember what the name of Setting was. +1 $\endgroup$ – Michael E2 Dec 8 '20 at 20:52

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.