2017-09-15 116 views
-4

我的數據我這個文件https://drive.google.com/open?id=0B4KXs5bh3CmPWXJkQWhkbzI0WEE如何在python熊貓中使用matplotlib繪製兩張圖?

#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'); 

這裏情節線的代碼我只得到用戶和客戶的數據我也想獲得的所有圖線是指兩個子和卡斯特小時總和weekdend和工作日

回答

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

我想繪製圖形,因爲我在圖片中顯示,但無法做 – kiran

+2

你能更明確地知道你試過什麼和出了什麼問題嗎?你的話建議在一張圖上的兩個圖是問題所在。 – doctorlove

+0

我更新了問題 – kiran

-1

這是一個公正的代碼,但是這是我怎麼這麼之前繪製

import matplotlib.pyplot as plt 

plt.plot(x-axis,y-axis,data) 
plt.plot(x-axis,y-axis,data) 

plt.show() 

,如果您繪製兩次都行會在同一個圖表。

+0

您可以使用上述數據繪製圖表,因爲我無法做到 – kiran

相關問題