Traceback lines on plot of multiple files

前端 未结 1 1682
悲&欢浪女
悲&欢浪女 2021-01-16 00:09

I am plotting data from multiple files. I do not want to use the glob module since I need to plot the data from each file separately. The data is plotting, but there are \'t

相关标签:
1条回答
  • 2021-01-16 00:43

    Your code is currently doing the following:

    1. Read data from a file, appending it to a list
    2. Plotting the list

    The list is not cleared at any point, so you keep plotting the list with more and more data appended to it, most of which is being plotted over and over. This is also why all your lines have the same color: it is the color of the last plot you made, which exactly covers all the previous plots and adds one more line.

    As it happens, pyplot has a nifty hold function that lets you ensure that any additional plots you make on a figure won't overwrite the old ones. You don't even need to generate your own color sequence. pyplot will do that for you too.

    While your program is functionally sound, there are also a few "stylistic" issues in your code that can be easily corrected. They are un-Pythonic at best and actually problematic at worst:

    1. Files should be closed after being opened. A context manager used in the with keyword is the standard approach for this.
    2. There are better ways to copy the result of os.listdir than a for loop. In fact, you don't need to copy the list at all.
    3. If you are writing a while loop that increments an index on every iteration, it should be a for loop.
    4. You never need a continue at the end of a loop. It is implied.

    So here is a solution that combines all of the above. This version assumes that you do not need to keep the contents of a given file around after you plot it:

    def graphWriterIRIandRut():
        # Set up the plots
        plt.subplot(2, 1, 1)
        plt.grid(True)
        plt.ylabel('IRI value', fontsize=12)
        plt.title('Right IRI data per mile for 2016 calibrations:')
        plt.tick_params(axis='both', which='major', labelsize=8)
        plt.hold(True)
    
        plt.subplot(2, 1, 2)
        plt.grid(True)
        plt.ylabel('IRI value', fontsize=12)
        plt.title('Left IRI data per mile for 2016 calibrations:')
        plt.tick_params(axis='both', which='major', labelsize=8)
        plt.hold(True)
    
        # Iterate over the files in the current directory
        for filename in os.listdir(os.getcwd()):
            # Initialize a new set of lists for each file
            startList = []
            endList = []
            iriRList = []
            iriLList = []
    
            # Load the file
            with open(filename, 'r') as file:
                for row in csv.DictReader(file):
                    startList.append(float(row['Start-Mi']))
                    endList.append(float(row[' End-Mi']))
                    iriRList.append(float(row[' IRI R e']))
                    iriLList.append(float(row['   IRI LWP']))
    
            # Add new data to the plots
            plt.subplot(2, 1, 1)
            plt.plot(startList, iriRList)
            plt.subplot(2, 1, 2)
            plt.plot(startList, iriLList)
    
        plt.show()
        plt.close('all')
    

    Running this function on the inputs you provided yields the following figure:

    For a more efficient way to work with CSVs and tabular data in general, you may want to check out the pandas library. It is a really powerful tool for analysis which includes plotting and IO routines for most of the use-cases you can probably imagine.

    0 讨论(0)
提交回复
热议问题