2015-06-22 29 views
1

我有看起來像下面的數據將數據作爲pandas.DataFrame熊貓數據框與重複序列:如何做一個意大利麪條?

        diff_1  diff_2 
1949-01-01 06:00:00    -0.555  -0.123 
1949-01-01 07:00:00    -0.654  0.230 
1949-01-02 06:00:00    -0.879  0.012 
1949-01-02 07:00:00    -0.459  0.672 
1949-01-03 06:00:00    -0.588  0.980 
1949-01-03 07:00:00    -0.068  0.375 
1950-01-01 06:00:00    -0.654  0.572 
1950-01-01 07:00:00    -0.544  0.092 
1950-01-02 06:00:00    0.374  -0.275 
1950-01-02 07:00:00    0.562  -0.260 
1950-01-03 06:00:00    -0.200  0.240 
1950-01-03 07:00:00    -0.226  0.202      

現在,我想做一個「意大利麪條式的情節」,這裏的「意大利麪條組」以一種顏色都determinated曲線是否diff_1或diff_2(所以x軸是從01-01到01-03的時間,y軸是差異,每個「意大利麪條」是一年)。 我試圖在東方這個問題:

Plot pandas data frame with year over year data

不過,我擔心我有一個維度太多。任何想法如何可以工作?

編輯:下面的簡單圖像說明了我在找什麼。一個顏色的多條線是由於x軸上的時間段每年重複一次。

enter image description here

+0

你能舉一個你想要表達的例子嗎?如果你正在試圖將你的數據框轉換成一些可繪製的東西,那麼如果它是關於繪圖或示例數據框的話,也許可以在繪畫中繪製一些東西。 – firelynx

+0

它是關於兩者。結果應該看起來像鏈接問題中的情節,只是在我的情況下,「diff_1」/「diff_2」出現在圖例中,並且一種顏色(代表年份)中有多個圖。 我已經添加了一個非常簡單的圖形來說明我的願望。 – user3017048

回答

1

這是我能做的最好的,我並不完全滿意,但它可能是不夠好:

# add a column with the year so you can pivot on it later. 
tdf = df.assign(year=df.index.year) 
# make all dates have the same year (a leap one just in case) 
tdf.index = df.index.map(lambda x: x.replace(year=2004)) 
# pivot using years as columns and put them in the topmost level. 
tdf = (tdf.pivot(columns='year').swaplevel(0, 1, axis='columns')) 
print(tdf) 

year     1949 1950 1949 1950 
        diff_1 diff_1 diff_2 diff_2 
2004-01-01 06:00:00 -0.555 -0.654 -0.123 0.572 
2004-01-01 07:00:00 -0.654 -0.544 0.230 0.092 
2004-01-02 06:00:00 -0.879 0.374 0.012 -0.275 
2004-01-02 07:00:00 -0.459 0.562 0.672 -0.260 
2004-01-03 06:00:00 -0.588 -0.200 0.980 0.240 
2004-01-03 07:00:00 -0.068 -0.226 0.375 0.202 

# create a list of as many colors as columns in df 
color = [c['color'] for c in plt.rcParams['axes.prop_cycle'][:df.columns.size]] 
# plot 
ax = plt.subplot() 
for year in tdf.columns.levels[0]: 
    tdf[year].plot(color=color, legend=False, ax=ax) 
plt.legend(ax.lines[:df.columns.size], df.columns, loc='best') 
plt.show() 

enter image description here

現在定製刻度標記你的心臟的內容。

+0

好的,的確,它似乎是一項重大的事業,但顯然它的工作原理! :-) – user3017048