[英]Heatmap with date on horizontal and hour on vertical axis (“fingerprint plot”)
I have a pandas data frame with a datetime index and some variable z
and I want to reproduce a plot similar to this: 我有一个带有日期时间索引和一些变量
z
的熊猫数据框,我想重现一个类似于此的图:
(Image source: University of Edinburgh, http://www.geos.ed.ac.uk/homes/rclement/micromet/Current/griffin/carbon/ )
(图片来源:爱丁堡大学, http : //www.geos.ed.ac.uk/homes/rclement/micromet/Current/griffin/carbon/ )
This is often called a "fingerprint plot" in the CO2 flux community. 在二氧化碳通量社区中,这通常被称为“指纹图”。
A year of sample data: 一年的样本数据:
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 do I proceed from here? 我如何从这里开始? I played around with the solution to this question: how to plot a heat map for three column data , but I can't get it to work with the datetime data.
我试着解决了这个问题的解决方案: 如何为三列数据绘制热图 ,但我无法使其与日期时间数据一起使用。
Does this get what you are looking for? 这能得到您想要的东西吗?
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.