【问题标题】:Filter by conditions and plot batch graphs in python在python中按条件过滤并绘制批处理图
【发布时间】:2020-06-26 19:05:46
【问题描述】:

我有一个数据集df,如下图:

    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         2
101 267 2019-11-28 15:44:34.027  27.5         3
102 273 2019-11-28 15:44:38.653  22.5         1
104 275 2019-11-28 15:44:38.653  22.5         2

我已经为按特定 group_id 和日期分组的数据块绘制了一个图表,例如。 group_id ==3,date =2020-01-01,使用以下代码:

df['timestamp'] = pd.to_datetime(df['timestamp'])

GROUP_ID = 2
df = df[df['group_id'] == GROUP_ID]

df['Date'] = [datetime.datetime.date(d) for d in df['timestamp']] 
df = df[df['Date'] == pd.to_datetime('2020-01-01')]      

df.plot(x='timestamp', y='data', figsize=(42, 16)) 
plt.axhline(y=40, color='r', linestyle='-')
plt.axhline(y=25, color='b', linestyle='-')

df['top_lim'] = 40
df['bottom_lim'] = 25

plt.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'])
plt.scatter(df['timestamp'][mask], df['data'][mask], marker='.', color='black')

cumulated_time = df['timestamp'][mask].diff().sum()
plt.gcf().subplots_adjust(left = 0.3)

plt.xlabel('Timestamp')
plt.ylabel('Data')
plt.show()

现在我想为每个date 绘制每个group_id 的图表。我该怎么做?有没有办法按这两个条件对数据进行分组并绘制图表?还是使用for-loop 更好?

【问题讨论】:

    标签: python datetime for-loop matplotlib plot


    【解决方案1】:

    使用 for 循环可以采取以下方法。假设每个组有 2 个日期,一个很好的绘图方法是有 2 列,行等于组数

    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)
    

    【讨论】:

    • 感谢 Sameeresque 的回答。代码或数据中没有groups
    • 您在问题中表示您want to plot a graph for eachgroup_id for each date
    • 是的。但是rows=len(groups) 中的groups 指的是什么? len('group_id')?
    • 您可以设置 groups=df['group_id']。这取决于您的用例。您可能需要对其进行修改以满足您的需要。
    • 好的。我也应该在for date in dates: 中定义dates 吗?
    猜你喜欢
    • 2020-08-26
    • 2018-01-09
    • 1970-01-01
    • 2019-05-28
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多