热图,水平日期和垂直轴小时("指纹图")

时间:2016-04-05 09:39:32

标签: python pandas plot heatmap

我有一个带有日期时间索引和一些变量z的pandas数据框,我想重现一个类似于此的图:

"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 个答案:

答案 0 :(得分: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()