Automate making multiple plots in python using several .csv files

前端 未结 1 1630
广开言路
广开言路 2020-12-20 09:12

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 l

相关标签:
1条回答
  • 2020-12-20 09:42

    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 folder
        if 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))
    
    0 讨论(0)
提交回复
热议问题