Skip to main content

15.9) Saving Your Plots


If you want to save you plot, you can use the GUI save button on the plot window.

Create a plot and save it in the location of your choice by clicking the save button.

For more flexibility or to automate saving your plot, you can use this command at the end of your script:

plt.savefig('testplot.png', dpi=300)

This will save your file to a png format with a resolution of 300 dpi (typically resolution requested for publications). If you save your plot you won’t need to show it, so remove the line with show() and replace it with a savefig() line.

Some journals request such figures to be saved as a vector graphic (eps, pdf) rather than a raster image with pixels (bmp, png, jpeg). In that case save your figure as an eps or pdf:

plt.savefig('testplot.eps')

If you want to adjust the plot size, you will need to add a line before the plot command:

plt.figure(1, figsize=(3,1), dpi=300)

This will make figure 1 of three inches wide and one inch high with a 300 dpi resolution.

Write a program that creates a plot and saves the graphic in the pdf format.