簡體   English   中英

熊貓 - 如何分組和繪制每周每天的每個小時

[英]Pandas - How to group-by and plot for each hour of each day of week

我需要幫助找出如何繪制子圖以便從我顯示的數據框中輕松比較:

  Date                   A        B         C              
2017-03-22 15:00:00     obj1    value_a    other_1
2017-03-22 14:00:00     obj2    value_ns   other_5
2017-03-21 15:00:00     obj3    value_kdsa other_23
2014-05-08 17:00:00     obj2    value_as   other_4
2010-07-01 20:00:00     obj1    value_as   other_0

我試圖繪制每周每個小時的每小時的出現次數。 因此,計算一周和每小時中每一天的出現次數,並將其繪制在如下所示的子圖上。

在此輸入圖像描述

如果這個問題聽起來很混亂,請告訴我您是否有任何疑問。 謝謝。

您可以使用多個groupby完成此操作。 由於我們知道一周有7天,我們可以指定面板數量。 如果你groupby(df.Date.dt.dayofweek) ,你可以使用組索引作為子圖軸的索引:

樣本數據

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

n = 10000
np.random.seed(123)
df = pd.DataFrame({'Date': pd.date_range('2010-01-01', freq='1.09min', periods=n),
                   'A': np.random.randint(1,10,n),
                   'B': np.random.normal(0,1,n)})

碼:

fig, ax = plt.subplots(ncols=7, figsize=(30,5))
plt.subplots_adjust(wspace=0.05)  #Remove some whitespace between subplots

for idx, gp in df.groupby(df.Date.dt.dayofweek):
    ax[idx].set_title(gp.Date.dt.day_name().iloc[0])  #Set title to the weekday

    (gp.groupby(gp.Date.dt.hour).size().rename_axis('Tweet Hour').to_frame('')
        .reindex(np.arange(0,24,1)).fillna(0)
        .plot(kind='bar', ax=ax[idx], rot=0, ec='k', legend=False))

    # Ticks and labels on leftmost only
    if idx == 0:
        _ = ax[idx].set_ylabel('Counts', fontsize=11)

    _ = ax[idx].tick_params(axis='both', which='major', labelsize=7,
                            labelleft=(idx == 0), left=(idx == 0))

# Consistent bounds between subplots. 
lb, ub = list(zip(*[axis.get_ylim() for axis in ax]))
for axis in ax:
    axis.set_ylim(min(lb), max(ub)) 

plt.show()

在此輸入圖像描述


如果您想使寬高比不那么極端,那么考慮繪制一個4x2網格。 一旦我們flatten軸陣列,它就像上面一樣非常相似。 有一些整數和余數除法來確定哪些axes需要標簽。

fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20,10))
fig.delaxes(ax[1,3])  #7 days in a week, remove 8th panel
ax = ax.flatten()  #Far easier to work with a flattened array

lsize=8
plt.subplots_adjust(wspace=0.05, hspace=0.15)  #Remove some whitespace between subplots

for idx, gp in df.groupby(df.Date.dt.dayofweek):
    ax[idx].set_title(gp.Date.dt.day_name().iloc[0])  #Set title to the weekday

    (gp.groupby(gp.Date.dt.hour).size().rename_axis([None]).to_frame()
        .reindex(np.arange(0,24,1)).fillna(0)
        .plot(kind='bar', ax=ax[idx], rot=0, ec='k', legend=False))

    # Titles on correct panels
    if idx%4 == 0:
        _ = ax[idx].set_ylabel('Counts', fontsize=11)
    if (idx//4 == 1) | (idx%4 == 3):
        _ = ax[idx].set_xlabel('Tweet Hour', fontsize=11) 

    # Ticks on correct panels
    _ = ax[idx].tick_params(axis='both', which='major', labelsize=lsize,
                            labelbottom=(idx//4 == 1) | (idx%4 == 3), 
                            bottom=(idx//4 == 1) | (idx%4 == 3),
                            labelleft=(idx%4 == 0), 
                            left=(idx%4 == 0))

# Consistent bounds between subplots. 
lb, ub = list(zip(*[axis.get_ylim() for axis in ax]))
for axis in ax:
    axis.set_ylim(min(lb), max(ub)) 

plt.show()

在此輸入圖像描述

seaborn怎么seaborn sns.FacetGrid就是這樣做的:

import pandas as pd
import seaborn as sns

# make some data
date = pd.date_range('today', periods=100, freq='2.5H')

# put in dataframe
df = pd.DataFrame({
    'date' : date
})

# create day_of_week and hour columns
df['dow'] = df.date.dt.day_name()
df['hour'] = df.date.dt.hour

# create facet grid
g = sns.FacetGrid(data=df.groupby([
    'dow',
    'hour'
]).hour.count().to_frame(name='day_hour_count').reset_index(), col='dow', col_order=[
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday'
], col_wrap=3)

# map barplot to each subplot
g.map(sns.barplot, 'hour', 'day_hour_count');

barplots

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM