从折线图创建1D热图

bs1*_*reh 3 linegraph matplotlib heatmap

是否可以从折线图中的数据创建一维热图?也就是说,我希望y中的最高值代表热图中较暖的颜色.我附上了我想要的热图的示例图像以及我目前在折线图中的数据.

1D热图和图示例:

在此输入图像描述

为了在显示的图像中获得热图,我使用matplotlib.pyplot在python中使用了以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

但我相信这只有在数据表示为散点图时才有效.

Imp*_*est 9

如果我们假设数据是等间隔的,则可以使用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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述