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?


2 Answers 2


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:

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

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

"plot_" <> ToString[i] <> ".png" can be replaced by StringTemplate["plot_``.png"] @i

  • 3
    $\begingroup$ 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" $\endgroup$
    – Szabolcs
    Jun 16, 2014 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.

  • $\begingroup$ neither did I. good one +1 $\endgroup$ Jan 15, 2013 at 20:46

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.