2015-06-22 48 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