0

I'm new to Pandas and attempting to loop through a directory of 8 identically-structured .txt files on my local drive, put each one into its own Pandas DataFrame and then plot each one as its own subplot.

The files have no header, and look like this (but with a lot more rows):

0.01 100
0.02 150
0.03 225

Here is my Python code:

import glob
import pandas as pd
from matplotlib import pyplot as plt

path = '/path/to/data/files/2015*' #dir has 8 files beginning with 2015
files = glob.glob(path)

for file in files:
    f = open(file, 'r')

    df = pd.read_csv(f, sep=' ', header=None, )

    fig = plt.figure(figsize=(10,8))
    subs = plt.subplots(8, sharex=True)    
    df.plot(subplots=True)
    plt.show()

What is weird is that it makes the structure of the 8 subplots, but it doesn't show any data plotted on them. Not sure why.

EDIT: I just noticed, when I read one of the files into a DataFrame as a test, it creates a df with the structure below:

0 0.01 100 N/A
1 0.02 150 N/A
2 0.03 225 N/A

It appears the code is adding N/A to each row as well as an index as the first column.

NaN
  • 643
  • 1
  • 8
  • 21

1 Answers1

0

You're doing several things eight times that you only need to do once: creating the figure, creating the subplots, and showing the graph. The only thing you need to do in each loop is select the right axes and plot the data.

Try this:

import glob
import pandas as pd
from matplotlib import pyplot as plt

path = '/path/to/data/files/2015*' #dir has 8 files beginning with 2015
files = glob.glob(path)

fig = plt.figure(figsize=(10,8))
subs, axes = plt.subplots(8, sharex=True)    

for i, file in enumerate(files):
    f = open(file, 'r')
    df = pd.read_csv(f, sep=' ', header=None, )

    plt.sca(axes[i])
    df.plot(subplots)

plt.show()
perigon
  • 2,160
  • 11
  • 16
  • Thanks! But this produces 8 separate figures. I would like just one figure with 8 subplots. – NaN Apr 08 '19 at 19:00