简体   繁体   English

python中的热图,用于表示给定矩形区域中的(x,y)坐标

[英]Heatmap in python to represent (x,y) coordinates in a given rectangular area

Let's say we have x,y coordinates as an input where x is in range(0,300) & y is in range(0,400) I want to plot all of these coordinates as a heat map in a rectangular grid of width between (0,300) & height between (0,400). 假设我们有x,y坐标作为输入,其中x在范围(0,300)和y在范围(0,400)中,我想将所有这些坐标作为热图绘制在宽度(0,300)与之间的矩形网格中高度介于(0,400)之间。

Using seaborn, or matplotlib, I'm able to plot a scatter plot, but struggling to plot these points as a heatmap. 使用seaborn或matplotlib,我能够绘制散点图,但努力将这些点绘制为热图。

x = numpy.random.randint(0, high=50, size=5000, dtype='l')
y = numpy.random.randint(0, high=50, size=5000, dtype='l')

Thus, if my sample size is 5000 points & all are nearly in the range of x as (0,50) & y as (0,50) representing them in a rectangular space of 300x400 should demonstrate the highest density of coordinates in 50x50 space. 因此,如果我的样本大小为5000点,并且所有点都在x的范围内(即(0,50)和y分别为(0,50)),则表示它们在300x400的矩形空间中应展示50x50空间中最高的坐标密度。

Can someone please guide me how to represent this data? 有人可以指导我如何表示此数据吗?

For testing & plotting on scatter plot, I used seaborn's lmplot function. 为了测试散点图并进行绘图,我使用了Seaborn的lmplot函数。

df = pd.DataFrame()

df['x'] = pd.Series(numpy.random.randint(0, high=320, size=5000, dtype='l'))
df['y'] = pd.Series(numpy.random.randint(0, high=480, size=5000, dtype='l'))
sns.set_style('whitegrid')
sns.lmplot('x','y',data=df,
       palette='coolwarm',size=10,fit_reg=False)
plt.show()

It seems that what is wanted here is a 2-dimensional histogram. 似乎这里需要的是二维直方图。 This can be plotted using plt.hist2d . 可以使用plt.hist2d进行绘制。

Example: 例:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rayleigh(50, size=5000)
y = np.random.rayleigh(50, size=5000)


plt.hist2d(x,y, bins=[np.arange(0,400,5),np.arange(0,300,5)])

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM