Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to generate a large number of figures as PNG files, and I'm trying to decide whether Mathematica would be the quickest way to do this.

All the figures have the same basic structure, and I'm confident that it will be easy to generate them in Mathematica, but I don't know how to automate the step that produces the PNG file. (This is an operation that I normally do from the GUI/frontend, but in this case there are too many images for this approach.)

So, to be a bit more specific, suppose that I have a function that, given the path to a file, will read that data in the file, and produce a graphic representation of it, what must I add to that function so that, instead of displaying the image in the frontend, it will save the corresponding PNG to disk?

share|improve this question
up vote 5 down vote accepted

Let's suppose that these are your imported data (three sets of random numbers):

importedData = Table[RandomReal[{0, 1}, {10, 2}], {3}];

Now we can directly produce and export the PNG files:

Table[Export["plot_" <> ToString[i] <> ".png", 
    "PNG"], {i, Length[importedData]}]

{"plot_1.png", "plot_2.png", "plot_3.png"}

share|improve this answer
IntegerString is useful because it makes it easy to specify the total number of digits and will insert the necessary number of leading zeros. For example, IntegerString[4, 10, 3] --> "004" – Szabolcs Jun 16 '14 at 21:24

To export a sequence of plots, you don't need to do the sequential numbering by hand (or by using ToString). Instead, there is the Export option "VideoFrames" that does the numbering automatically:

plots = 
  Table[Plot[Sin[x + a], {x, 0, Pi}, 
    PlotRange -> {{0, Pi}, {-1.1, 1.1}}], {a, 0, Pi, .1}];


Export["plot001.png", plots, "VideoFrames"];


==> {"plot001.png", "plot002.png", "plot003.png", "plot004.png", \
"plot005.png", "plot006.png", "plot007.png", "plot008.png", \
"plot009.png", "plot010.png", "plot011.png", "plot012.png", \
"plot013.png", "plot014.png", "plot015.png", "plot016.png", \
"plot017.png", "plot018.png", "plot019.png", "plot020.png", \
"plot021.png", "plot022.png", "plot023.png", "plot024.png", \
"plot025.png", "plot026.png", "plot027.png", "plot028.png", \
"plot029.png", "plot030.png", "plot031.png", "plot032.png"}


The number of digits in the numbered output files is determined from the number you specify in the first file name.

share|improve this answer
+1, did not know about "VideoFrames". – rcollyer Jan 15 '13 at 16:09
neither did I. good one +1 – Mike Honeychurch Jan 15 '13 at 20:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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