BRK92
BRK92

Reputation: 13

How can I loop through a list of elements and create time series plots in Python

Here is a sample of the data I'm working with WellAnalyticalData I'd like to loop through each well name and create a time series chart for each parameter with sample date on the x-axis and the value on the y-axis. I don't think I want subplots, I'm just looking for individual plots of each analyte for each well. I've used pandas to try grouping by well name and then attempting to plot, but that doesn't seem to be the way to go. I'm fairly new to python and I think I'm also having trouble figuring out how to construct the loop statement. I'm running python 3.x and am using the matplotlib library to generate the plots.

Upvotes: 0

Views: 1009

Answers (1)

Pol Molinas
Pol Molinas

Reputation: 56

so if I understand your question correctly you want one plot for each combination of Well and Parameter. No subplots, just a new plot for each combination. Each plot should have SampleDate on the x-axis and Value on the y-axis. I've written a loop here that does just that, although you'll see that since in your data has just one date per well per parameter, the plots are just a single dot.

    import pandas as pd
    import matplotlib.pyplot as plt
    %matplotlib inline

    df = pd.DataFrame({'WellName':['A','A','A','A','B','B','C','C','C'],
                 'SampleDate':['2018-02-15','2018-03-31','2018-06-07','2018-11-14','2018-02-15','2018-11-14','2018-02-15','2018-03-31','2018-11-14'],
               'Parameter':['Arsenic','Lead','Iron','Magnesium','Arsenic','Iron','Arsenic','Lead','Magnesium'],
               'Value':[0.2,1.6,0.05,3,0.3,0.79,0.3,2.7,2.8]
              })

    for well in df.WellName.unique():
        temp1 = df[df.WellName==well]
        for param in temp1.Parameter.unique():
            fig = plt.figure()
            temp2 = temp1[temp1.Parameter==param]
            plt.scatter(temp2.SampleDate,temp2.Value)
            plt.title('Well {} and Parameter {}'.format(well,param))

Upvotes: 1

Related Questions