Skip to content Skip to sidebar Skip to footer

Filter By Conditions And Plot Batch Graphs In Python

I have a dataset df as shown below: id timestamp data group_id 99 265 2019-11-28 15:44:34.027 22.5 1 100 266 2019-11-28 15:44:34.027 23.5

Solution 1:

Using for-loop you can take the following approach. Assuming that for each group you have 2 dates, a nice way to plot would be to have 2 columns, and rows equal to the number of groups

rows=len(groups) #set the desired number of rows
cols=2 #set the desired number of columns

fig, ax = plt.subplots(rows, cols, figsize=(13,8),sharex=False,sharey=False) # if you want to turn off sharing axis.
g=0 #to iterate over rows/cols
d=0 #to iterate over rows/cols
for group in groups:
    for date in dates:
        GROUP_ID = group
        df = df[df['group_id'] == GROUP_ID]
        df['Date'] = [datetime.datetime.date(d) for d in df['timestamp']] 
        df = df[df['Date'] == date]      
        df.plot(x='timestamp', y='data', figsize=(42, 16)) 
        ax[g][d].axhline(y=40, color='r', linestyle='-')
        ax[g][d].axhline(y=25, color='b', linestyle='-')
        df['top_lim'] = 40
        df['bottom_lim'] = 25
        ax[g][d].fill_between(df['timestamp'], df['bottom_lim'], df['data'],
                        where=(df['data'] >= df['bottom_lim'])&(df['data'] <= df['top_lim']),
                        facecolor='orange', alpha=0.3)
        mask = (df['data'] <= df['top_lim'])&(df['data'] >= df['bottom_lim'])
        ax[g][d].scatter(df['timestamp'][mask], df['data'][mask], marker='.', color='black')
        cumulated_time = df['timestamp'][mask].diff().sum()

        d=d+1
        if d==1:
            g=g
        else:
            g=g+1
            d=0


fig.text(0.5, -0.01, 'Timestamp', ha='center', va='center',fontsize=20)
fig.text(-0.01, 0.5, 'Data', ha='center', va='center', rotation='vertical',fontsize=20)
plt.subplots_adjust(left = 0.3)

Post a Comment for "Filter By Conditions And Plot Batch Graphs In Python"