2017-08-23 227 views
0

是否可以从线图中的数据创建一维热图?即我希望y中的最高值代表热图中较暖的颜色。我附上了热图的示例图像,我希望它看起来像我目前在折线图中的数据一样。从折线图创建一维热图

1D热图和图表例如:

enter image description here

为了得到I所示用下面的代码在python与matplotlib.pyplot图像中的热图:

heatmap, xedges, yedges = np.histogram2d(x, y, bins=(np.linspace(0,length_track,length_track+1),1)) 
extent = [0, length_track+1, 0, 50] 
plt.imshow(heatmap.T, extent=extent, origin='lower', cmap='jet',vmin=0,vmax=None) 

但是我相信这只适用于数据表示为散点图的情况。

+0

如何直方图相关的问题吗?它是直方图数据,你想显示为热图?您可能想要更精确地指定要显示的数据。 – ImportanceOfBeingErnest

回答

4

如果我们假设数据间隔相等,则可以使用imshow图来重新创建问题图。

import matplotlib.pyplot as plt 
import numpy as np; np.random.seed(1) 
plt.rcParams["figure.figsize"] = 5,2 

x = np.linspace(-3,3) 
y = np.cumsum(np.random.randn(50))+6 

fig, (ax,ax2) = plt.subplots(nrows=2, sharex=True) 

extent = [x[0]-(x[1]-x[0])/2., x[-1]+(x[1]-x[0])/2.,0,1] 
ax.imshow(y[np.newaxis,:], cmap="plasma", aspect="auto", extent=extent) 
ax.set_yticks([]) 
ax.set_xlim(extent[0], extent[1]) 

ax2.plot(x,y) 

plt.tight_layout() 
plt.show() 

enter image description here