按条件过滤并在python中绘制批处理图 [英] Filter by conditions and plot batch graphs in python

查看:35
本文介绍了按条件过滤并在python中绘制批处理图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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         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,日期= 2020-01-01 ,使用以下代码:

I have plotted a graph for a chunk of data grouped by a particular group_id and date, eg. group_id ==3, date =2020-01-01, using code below:

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()

现在,我想为每个日期每个 group_id 绘制图形.我该怎么做?有没有一种方法可以根据这两个条件对数据进行分组并绘制图表?还是使用 for-loop 更好?

Now I want to plot a graph for eachgroup_id for each date. How can I do it? Is there a way to group data by these two conditions, and plot the graphs? Or is it better to use a for-loop?

推荐答案

使用for循环,您可以采用以下方法.假设每个组有2个日期,则绘制一个好方法是有2列,且行等于组数

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)

这篇关于按条件过滤并在python中绘制批处理图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆