从折线图创建一维热图 [英] Creating a 1D heat map from a line graph

查看:173
本文介绍了从折线图创建一维热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据折线图中的数据创建一维热图?即我希望y中的最大值表示热图中的暖色.我已经附加了一个热图示例图像,我希望它看起来像现在在折线图中的数据一样.

Is it possible to create a 1D heat map from data in a line graph? i.e. I'd like the highest values in y to represent the warmer colours in a heat map. I've attached an example image of the heat map I'd like it to look like as well as data I currently have in the line graph.

一维热图和图形示例:

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

To get the heatmap in the image shown I used the following code in python with 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)

但是我相信这仅在数据以散点图表示的情况下有效.

But I believe this only works if the data is represented as a scatter plot.

推荐答案

如果我们假设数据等距分布,则可以使用imshow绘图从问题中重新创建绘图.

If we assume that the data is equally spaced, one may use an imshow plot to recreate the plot from the question.

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()

这篇关于从折线图创建一维热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆