ekajb
ekajb

Reputation: 1

Plot multiple csv files with Python/matplotlib loop

I have a directory filled with multiple .csv files, each only has two columns (date and an integer). I am trying to get this code to loop over each file and plot them individually so that there is a corresponding .png to each .csv. Every time it runs, I end up with the correct number of .png files, but each has exactly the same data. I have already inplemented the plt.clf() method to clear it for each loop, but it doesn't work. Here is the code:

import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import datetime as DT
import matplotlib.dates as mdates
import scipy
import os
import glob

rootdir='/path/to/file'

for infile in glob.glob( os.rootdir.join(rootdir, '*.csv.out') ):
    output = infile + '.out'

data= np.loadtxt(infile, delimiter=',',
         dtype={'names': ('date', 'session'),'formats': ('S10', 'i4')} )

#Organizes 2-column spreadsheet
dates, sessions = map(list, zip(*data))
print dates, sessions

x = [DT.datetime.strptime(date,"%m-%d-%y") for date in dates]
y = [sessions]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis_date()
ax.grid()
#Fills space under plotted line
ax.fill_between(x, sessions, color='blue')

# slants the x axis
fig.autofmt_xdate()
plt.plot(x,sessions)
plt.xlabel('Date')
plt.ylabel('Sessions')
plt.title('Peak Usage')

fileNameTemplate = r'\path\to\file\Plot{}.png'

for subdir,dirs,files in os.walk(rootdir):
    for count, file in enumerate(files):
        pl.savefig(fileNameTemplate.format(count), format='png')
        pl.clf() 

I modeled the enumerator after a solution in this answer but I am still getting an issue.

Upvotes: 0

Views: 1877

Answers (1)

mauve
mauve

Reputation: 2763

You need to:

  • define a function for your plots
  • call that function from your loop
  • include plt.close() at the end of said function.

Right now, you're not creating new plots as you walk the directory. The plot command needs to be inside the loop.

def plot():
   #do your plotting in here. If this is being called from a loop and the 
   #variables used herein are defined before, it will use the 
   #global values as they exist at the time. You can also end this function with
  fig.savefig(**args)
  plt.close()



for count, file in enumerate(files):
    plot()        

Upvotes: 1

Related Questions