问题
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
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()
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?
回答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)
来源:https://stackoverflow.com/questions/60692929/filter-by-conditions-and-plot-bulk-graphs-in-python