Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving multiple plots

i have this code to produce multiple plots from all the text files in a folder. It runs perfectly fine and shows the plots but i cant work out how to then save them all.

import re
import numpy as np
import matplotlib.pyplot as plt
import pylab as pl
import os

rootdir='C:\documents\Neighbors for each search id'

for subdir,dirs,files in os.walk(rootdir):
 for file in files:
  f=open(os.path.join(subdir,file),'r')
  print file
  data=np.loadtxt(f)

  #plot data
  pl.plot(data[:,1], data[:,2], 'gs')

  #Put in the errors
  pl.errorbar(data[:,1], data[:,2], data[:,3], data[:,4], fmt='ro')

  #Dashed lines showing pmRa=0 and pmDec=0
  pl.axvline(0,linestyle='--', color='k')
  pl.axhline(0,linestyle='--', color='k')
  pl.show()

  f.close()

I have previously used

fileName="C:\documents\FirstPlot.png"
plt.savefig(fileName, format="png")

but i think this just saves each graph into one file and overwrites the last one.

like image 549
user1841859 Avatar asked Nov 25 '12 20:11

user1841859


People also ask

How do I save multiple plots in python?

MatPlotLib with Python Create a new figure (fig2) or activate an existing figure using figure() method. Plot the Second line using plot() method. Initialize a variable, filename, to make a pdf file. Create a user-defind function, save_multi_image, and call it to save all the open matplotlib figures in one file at once.

How do I save multiple plots as an image in Python?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.

How do you save multiple plots in Jupyter notebook?

You can output each plot as an image, maybe into a new, separate directory, in the course of running your notebook and then at the end of the notebook code a section in using ReportLab or Pillow to iterate on the images in your directory to composite them together as you wish.

Can you have multiple plots?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.


1 Answers

All you have to do is provide unique filenames. You could use a counter:

fileNameTemplate = r'C:\documents\Plot{0:02d}.png'

for subdir,dirs,files in os.walk(rootdir):
    for count, file in enumerate(files):
        # Generate a plot in `pl`
        pl.savefig(fileNameTemplate.format(count), format='png')
        pl.clf()  # Clear the figure for the next loop

What I did:

  • Create a template using python's string formatting syntax

  • Added a counter to the loop using the enumerate() function.

  • Used the counter and the template to generate a new filename for each plot.

like image 129
Martijn Pieters Avatar answered Oct 17 '22 11:10

Martijn Pieters