2016-04-05 29 views
0

我有一個日期時間索引和一些可變z一個熊貓數據幀和欲複製類似這樣的曲線圖:熱圖與日期上的水平和小時的垂直軸(「指紋情節」)

"Fingerprint plot" of CO2 fluxes (圖片來源:愛丁堡大學,http://www.geos.ed.ac.uk/homes/rclement/micromet/Current/griffin/carbon/

這通常被稱爲CO2通量社區的「指紋圖」。

一年的樣本數據:

import pandas as pd 
import numpy as np 
n = 366*24 
df = pd.DataFrame(index=pd.date_range(start='2016-01-01 00:00', freq='H', periods=n), 
        data={'z': np.random.randn(n)}) 
df["x"] = df.index.date 
df["y"] = df.index.hour 
df.head() 

如何從這裏着手?我玩過這個問題的解決方案: how to plot a heat map for three column data,但我不能讓它與日期時間數據一起工作。

回答

1

這是否得到您要找的內容?

from scipy.interpolate import griddata 
from jdcal import jd2jcal 
from datetime import datetime 

n = 366*24 
df = pd.DataFrame(index=pd.date_range(start='2016-01-01 00:00', freq='H', periods=n), 
        data={'z': np.random.randn(n)}) 
df["x"] = df.index.date 
df["y"] = df.index.hour 
df.head() 

xi = np.linspace(df.index.to_julian_date().min(), df.index.to_julian_date().max(), 1000) 
yi = np.linspace(df.y.min(), df.y.max(), 1000) 
zi = griddata((df.index.to_julian_date(),df.index.hour),df.z,(xi[None,:],yi[:,None]),method='linear') 
xij = [jd2jcal(0,v) for v in xi] 
xid = [datetime(x[0],x[1],x[2]) for x in xij] 
plt.contourf(xid,yi,zi) 
plt.colorbar() 
plt.show() 
+0

正是我需要的,謝謝! –