我有这个文件https://drive.google.com/open?id=0B4KXs5bh3CmPWXJkQWhkbzI0WEE
的数据#count weekdays 0 to 4
mUserType = np.where(rides['starttime'].dt.dayofweek <= 5, 'Weekend',
'Weekday')
#count hours
mWeekHours = rides['starttime'].dt.hour
#create group by usertype
WeekdayWorkdayResult = rides.groupby([mUserType, mWeekHours,
'usertype']).size().unstack()
WeekdayWorkdayResult
此代码根据用户类型(即客户和订阅者)计算周末和工作日的小时数
我想用这种格式使用matplotlib绘制图表我该怎么办?Weekday and Weekend Graph
显示按小时和用户类型划分的游乐设施数量。工作日的一个情节和周末的第二个情节。包括所有每小时游乐设施的情节线
# count trips by date and by hour
ind = pd.DatetimeIndex(rides.starttime)
rides['date'] = ind.date.astype('datetime64')
rides['hour'] = ind.hour
by_hour = rides.pivot_table('tripduration', aggfunc='count',
index=['date', 'hour'],
columns='usertype').fillna(0).reset_index('hour')
# average these counts by weekend
by_hour['weekend'] = (by_hour.index.dayofweek >= 5)
by_hour = by_hour.groupby(['weekend', 'hour']).mean()
by_hour.index.set_levels([['weekday', 'weekend'],
["{0}:00".format(i) for i in range(24)]],
inplace=True);
by_hour.columns.name = None
fig, ax = plt.subplots(1, 2, figsize=(16, 6), sharey=True)
by_hour.loc['weekday'].plot(title='Hourly Rides User Type and Hour',
ax=ax[0])
by_hour.loc['weekend'].plot(title='Weekend Rides by User Type and Hour',
ax=ax[1])
ax[0].set_ylabel('Hourly Rides by User Type');
以下是我只获得订阅者和客户数据的代码我还希望得到所有图表行,表示subnd和cust for weekdend和workdays的小时数
答案 0 :(得分:0)
您可以在一个地块上使用subplot
许多图表。
告诉它有多少行和列,以及您正在绘制的内容。 有一个演示here
import matplotlib.pyplot as plt
#set up data
plt.subplot(2, 1, 1) #two rows, one columns, first graph
#Call plt.plot(x1, y1) with the data you want
plt.title('First subplot')
plt.subplot(2, 1, 2) #two rows, one columns, 2nd graph
#Call plt.plot(x2, y2) with the data you want
plt.title('Second subplot')
plt.show()
答案 1 :(得分:0)
这是一个公正的代码,但这就是我的绘图方式
{{1}}
所以之前,如果你绘制两次,那么这条线将在同一个图中。