2016-05-17 128 views
0

我有一個以時間軸爲索引的熊貓數據框。當繪製下面的代碼如何在時間軸上正確放置seaborn/matplotlib中的x軸?

%matplotlib inline 
import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 
import seaborn as sns; sns.set(); 
ids = [1,2,3,4] 
dates = pd.date_range('20150704', '20160331') 
vals = np.random.randn(len(ids)*len(dates)) 
allids = np.tile(ids, len(dates)) 
alldates = np.tile(dates, len(ids)) 
df = (pd.DataFrame(np.vstack((allids, vals)).T, index=alldates) 
    .reset_index().rename(columns={'index':'datetime',0:'unique_id',1:'height'})) 

time_group = '1w' 
threshold = 0.50 

fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(12, 16), sharex=True) 

for (i, temp), ax in zip(df.groupby('unique_id'), axes.ravel()): 
    (temp.set_index('datetime').height 
     .groupby(pd.TimeGrouper(time_group)) 
     .mean() 
     .plot(ax=ax)) 

我得到了一個有4個地塊的情節。在x軸的垂直線的間隔是這樣的:

wrong vertical spacing

通知如何,例如,十一月比臘寬(因此它不是僅僅天的各月的數目)。顯然這是由於每個月的數據點數量,而不是每個月的天數。任何建議,以適當的時間正確的月份開始/月底的垂直線間隔?這是一個錯誤?

回答

0

1W將根據每個月的星期日分組,並且有幾個月我們有更多的星期日與其他人相比......這就是爲什麼有些月份更寬。下面的改變應該有所幫助...

%matplotlib inline 
    import matplotlib.pyplot as plt 
    import numpy as np 
    import pandas as pd 
    import matplotlib.dates as mdates 
    import seaborn as sns; sns.set(); 
    ids = [1,2,3,4] 
    dates = pd.date_range('20150704', '20160331') 
    vals = np.random.randn(len(ids)*len(dates)) 
    allids = np.tile(ids, len(dates)) 
    alldates = np.tile(dates, len(ids)) 
    df = (pd.DataFrame(np.vstack((allids, vals)).T, index=alldates) 
     .reset_index().rename(columns={'index':'datetime',0:'unique_id',1:'height'})) 
    time_group = '1w' 
    threshold = 0.50 

    fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(12, 16), sharex=True) 


    for (i, temp), ax in zip(df.groupby('unique_id'), axes.ravel()): 
     b=(temp.set_index('datetime').height 
      .groupby(pd.TimeGrouper(time_group)) 
      .mean()) 
     ax.plot(b.index,b) 
     ax.xaxis.set_major_formatter(mdates.DateFormatter('%b')) 
    plt.show()