Group Abstract Group Abstract

Message Boards Message Boards

How to export a graph produced by Manipulate - with CURRENT parameters

GROUPS:
Suppose I have a graph produced by Manipulate, for example
Manipulate[Plot[Sin[k x], {x, 0, 6}], {k, 1, 10}]
and would like to produce a pdf file of the graph AFTER moving the k parameter slider. That is, I'd like to have a snapshot of what is actually being shown on the screen with the CURREN value of k. Trying to export directly with
Export["graph.pdf", %]
does not work - the graph produced is that for the initial value k = 1 and NOT for the current value of k showing on the screen. Is there some setting I can make to force it to produce the current view?
POSTED BY: Boris Blankleider
Answer
1 year ago
There are 2 things here. The first is that the file name needs to be sorted out, using some kind of counter. Else you'll be simply overwriting the same pdf file over and over all the time.

The second issue, is that I believe there is a bug in the export inside Manipulate. This was brought up before. Here is a link

Export[] in Manipulate, infinite loop

Putting export inside Manipulate, causes infinite loop. Here is an example of how to see this problem. I made a Manipulate which exports the current plot. It uses a counter so the name can be seen clearly from the file system. You can see that, when you run this, that a new PDF file is being created, each with new name, however, this is done without moving the slider.

Here is the code
 Manipulate[
  Module[{x, g, fileName},
   g = Plot[Sin[k x], {x, 0, 6}];
   fileName = "graph_" <> ToString[++count] <> ".pdf";
   Export[fileName, Grid[{{"k=", k}, {g, SpanFromLeft}}]];
   g
   ],
  {k, 1, 10},
  {{count, 0}, None},
TrackedSymbols :> {k},

SynchronousUpdating -> True,
ContinuousAction -> False
]

Someone from WRI should really look at this issue. If you see the same problem, I suggest to send a bug report to support@wolfram.com just in case.  Watch out when you run this code, as the file will keep on being created all the time. So need to stop the Manipulate (delete the cell) to stop the files growing (else you'll run out of disk space).
POSTED BY: Nasser
Answer
1 year ago
If appearance of controls is irrelevant, then you can always use "snapshot" functionality. Once you have your graph as pasted snap shot it is easy to export exactly as it is.



POSTED BY: Vitaliy Kaurov
Answer
1 year ago
Just in case I will aslo offer another solution that I myself use often. From my point of view an ideal snipet of code that does the job conveniently with a click of a button is ( note the part {plt, ControlType -> None} localizes variable plt so it won't leak outside Manipulate ):
Manipulate[
plt = Plot[Sin[k x], {x, 0, 6}],
Button["Export", Export["plot" <> ToString[k] <> ".pdf", plt]],
{plt, ControlType -> None},
{k, 1, 10}]

POSTED BY: Vitaliy Kaurov
Answer
1 year ago
I am VERY happy with Vitaliy's solutions - there are exactly what I was looking for! Thank you, spasibo, Vitaliy!
POSTED BY: Boris Blankleider
Answer
1 year ago