【问题标题】:Creating a 1D heat map from a line graph从折线图创建一维热图
【发布时间】:2018-02-01 03:58:36
【问题描述】:

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

一维热图和图表示例:

为了获得图中所示的热图,我在 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)

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

【问题讨论】:

  • 直方图与问题有什么关系?是要显示为热图的直方图数据吗?您可能希望更准确地指定要显示的数据。

标签: matplotlib heatmap linegraph


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多