Skip to content Skip to sidebar Skip to footer

Automate Making Multiple Plots In Python Using Several .csv Files

I have 14 .csv files (1 .csv file per location) that will be used to make a 14 bar plots of daily rainfall. The following code is an example of what one bar plot will look like. i

Solution 1:

First method: you need to put all your csv files in the current folder. You also need to use the os module.

import os
for f in os.listdir('.'):                 # loop through all the files in your current folderif f.endswith('.csv'):                # find csv files
        fn, fext = os.path.splitext(f)    # split file name and extension

        dat = pd.read_csv(f)              # import data# Run the rest of your code here

        plt.savefig('{}.jpg'.format(fn))  # name the figure with the same file name 

Second method: if you don't want to use the os module, you can put your file names in a list like this:

files = ['a.csv', 'b.csv']

for f in files:
    fn = f.split('.')[0]

    dat = pd.read_csv(f)
    # Run the rest of your code here

    plt.savefig('{}.jpg'.format(fn))

Post a Comment for "Automate Making Multiple Plots In Python Using Several .csv Files"