2014-04-01 65 views
14

時間序列數據說我用下面創建一個完全隨機Dataframe繪製與seaborn

from pandas.util import testing 
from random import randrange 

def random_date(start, end): 
    delta = end - start 
    int_delta = (delta.days * 24 * 60 * 60) + delta.seconds 
    random_second = randrange(int_delta) 
    return start + timedelta(seconds=random_second) 

def rand_dataframe(): 
    df = testing.makeDataFrame() 
    df['date'] = [random_date(datetime.date(2014,3,18),datetime.date(2014,4,1)) for x in xrange(df.shape[0])] 
    df.sort(columns=['date'], inplace=True)  
    return df 

df = rand_dataframe() 

這導致在這篇文章的底部顯示的數據幀。我想用timeseries可視化功能繪製我的專欄ABCDseaborn,使我得到的東西沿着這些路線:

enter image description here

我怎樣才能解決這個問題?從我上this notebook閱讀,通話應該是:

sns.tsplot(df, time="time", unit="unit", condition="condition", value="value") 

但這似乎需要將數據幀被以不同的方式表示,其中列莫名其妙編碼timeunitconditionvalue,這是不我的情況。我如何將我的數據框(如下所示)轉換爲這種格式?

這裏是我的數據框:

 date   A   B   C   D 

2014-03-18 1.223777 0.356887 1.201624 1.968612 
2014-03-18 0.160730 1.888415 0.306334 0.203939 
2014-03-18 -0.203101 -0.161298 2.426540 0.056791 
2014-03-18 -1.350102 0.990093 0.495406 0.036215 
2014-03-18 -1.862960 2.673009 -0.545336 -0.925385 
2014-03-19 0.238281 0.468102 -0.150869 0.955069 
2014-03-20 1.575317 0.811892 0.198165 1.117805 
2014-03-20 0.822698 -0.398840 -1.277511 0.811691 
2014-03-20 2.143201 -0.827853 -0.989221 1.088297 
2014-03-20 0.299331 1.144311 -0.387854 0.209612 
2014-03-20 1.284111 -0.470287 -0.172949 -0.792020 
2014-03-22 1.031994 1.059394 0.037627 0.101246 
2014-03-22 0.889149 0.724618 0.459405 1.023127 
2014-03-23 -1.136320 -0.396265 -1.833737 1.478656 
2014-03-23 -0.740400 -0.644395 -1.221330 0.321805 
2014-03-23 -0.443021 -0.172013 0.020392 -2.368532 
2014-03-23 1.063545 0.039607 1.673722 1.707222 
2014-03-24 0.865192 -0.036810 -1.162648 0.947431 
2014-03-24 -1.671451 0.979238 -0.701093 -1.204192 
2014-03-26 -1.903534 -1.550349 0.267547 -0.585541 
2014-03-27 2.515671 -0.271228 -1.993744 -0.671797 
2014-03-27 1.728133 -0.423410 -0.620908 1.430503 
2014-03-28 -1.446037 -0.229452 -0.996486 0.120554 
2014-03-28 -0.664443 -0.665207 0.512771 0.066071 
2014-03-29 -1.093379 -0.936449 -0.930999 0.389743 
2014-03-29 1.205712 -0.356070 -0.595944 0.702238 
2014-03-29 -1.069506 0.358093 1.217409 -2.286798 
2014-03-29 2.441311 1.391739 -0.838139 0.226026 
2014-03-31 1.471447 -0.987615 0.201999 1.228070 
2014-03-31 -0.050524 0.539846 0.133359 -0.833252 

最後,我所尋求的是曲線的疊加(每列一個),每個人看起來如下(注意,不同的價值觀CI得到阿爾法值不同):

                                        enter image description here

+0

你」你的索引中有重複的日期。故意的?如果是的話,那有什麼意義呢? –

+0

Thanks @PaulH這是故意的,雖然他們可以移動到一列。我在每個日期有多個樣本,我想要捕捉該圖中帶中的每個日期的變化。 –

+0

所以要冗長,線條本身來自給定日期的平均值,而陰影帶則由最小值和最大值限定? –

回答

34

我不認爲tsplot會與你擁有的數據的工作。它對輸入數據的假設是,你在每個時間點採樣了相同的單位(儘管某些單位可能缺少時間點)。例如,假設你每天測量同一個人的血壓一個月,然後你想通過條件來繪製平均血壓(其中可能「條件」變量是他們所在的飲食)。 tsplot能做到這一點,與調用看起來像sns.tsplot(df, time="day", unit="person", condition="diet", value="blood_pressure")

那種情況下是有不同的飲食一大羣人,每天隨機從每組採樣一些並測量他們的血壓有所不同。從你給出的例子來看,你的數據看起來就像這樣。

然而,這並不難拿出matplotlib和熊貓的混合,會做什麼,我想你想:

# Read in the data from the stackoverflow question 
df = pd.read_clipboard().iloc[1:] 

# Convert it to "long-form" or "tidy" representation 
df = pd.melt(df, id_vars=["date"], var_name="condition") 

# Plot the average value by condition and date 
ax = df.groupby(["condition", "date"]).mean().unstack("condition").plot() 

# Get a reference to the x-points corresponding to the dates and the the colors 
x = np.arange(len(df.date.unique())) 
palette = sns.color_palette() 

# Calculate the 25th and 75th percentiles of the data 
# and plot a translucent band between them 
for cond, cond_df in df.groupby("condition"): 
    low = cond_df.groupby("date").value.apply(np.percentile, 25) 
    high = cond_df.groupby("date").value.apply(np.percentile, 75) 
    ax.fill_between(x, low, high, alpha=.2, color=palette.pop(0)) 

此代碼生成:

enter image description here

+0

謝謝。你爲什麼認爲'tsplot'不行?我可以理解,統計假設不會結轉,但「tsplot」如何知道樣本來自哪裏?它是否假設每個日期的元素數量不變? –

+1

這就是'unit'參數的作用 - 你告訴它每個樣本對應哪個單位,然後在那裏它表示每個單位將代表每個時間點。 – mwaskom

+0

我之所以問,是因爲我特別感興趣的是'tsplot'對置信區間的不同值使用不同的alpha值的能力(我更新了OP以在最後突出顯示) –