[英]How to do subplot for each month?
I have grouped the dataset by month and date and I have added third column for count the data in each day. 我按月和日期对数据集进行了分组,并添加了第三列来计算每天的数据。
Dataframe before 之前的数据帧
month day
0 1 1
1 1 1
2 1 1
..
3000 12 31
3001 12 31
3002 12 31
Dataframe now: 数据帧现在:
month day count
0 1 1 300
1 1 2 500
2 1 3 350
..
363 12 28 700
364 12 29 1300
365 12 30 1000
How to do subplot for each month , x will be the days and y will be the count 如何做每个月的子图,x将是日,y将是计数
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df= pd.read_csv('/home/rand/Downloads/Flights.csv')
by_month= df.groupby(['month','day']).day.agg('count').to_frame('count').reset_index()
I'm beginner in data science field 我是数据科学领域的初学者
I think you could use pandas.DataFrame.pivot
to change the shape of your table to make it more convenient for the plot. 我认为您可以使用pandas.DataFrame.pivot
来更改表格的形状,使其更方便绘图。 So in your code you could do something like this: 所以在你的代码中你可以这样做:
plot_data= df.pivot(index='day', columns='month', values='count')
plot_data.plot()
plt.show()
This is assuming you have equal number of days in every month since in the sample you included, month 12 only has 30 days. 这假设您每月的天数相同,因为在您包含的样本中,第12个月只有30天。 More on pivot . 更多关于枢轴 。
Try this 尝试这个
fig, ax = plt.subplots()
ax.set_xticks(df['day'].unique())
df.groupby(["day", "month"]).mean()['count'].unstack().plot(ax=ax)
Above code will give you 12 lines representing each month in one plot. 上面的代码将为您提供12行代表每个月的一个图。 If you want to have 12 individual subplots for those months, try this: 如果您希望在这几个月内有12个单独的子图,请尝试以下方法:
fig = plt.figure()
for i in range(1,13):
df_monthly = df[df['month'] == i] # select dataframe with month = i
ax = fig.add_subplot(12,1,i) # add subplot in the i-th position on a grid 12x1
ax.plot(df_monthly['day'], df_monthly['count'])
ax.set_xticks(df_monthly['day'].unique()) # set x axis
Try this: 尝试这个:
df = pd.DataFrame({
'month': list(range(1, 13))*3,
'days': np.random.randint(1,11, 12*3),
'count': np.random.randint(10,20, 12*3)})
df.set_index(['month', 'days'], inplace=True)
df.sort_index()
df = df.groupby(level=[0, 1]).sum()
Code to plot it: 绘制代码:
df.reset_index(inplace=True)
df.pivot(index='days', columns='month', values='count').fillna(0).plot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.