使用散点数据集在 MatPlotLib 中生成热图 [英] Generate a heatmap in MatPlotLib using a scatter data set

查看:46
本文介绍了使用散点数据集在 MatPlotLib 中生成热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组 X、Y 数据点(大约 10k),它们很容易绘制为散点图,但我想将其表示为热图.

I have a set of X,Y data points (about 10k) that are easy to plot as a scatter plot but that I would like to represent as a heatmap.

我浏览了 MatPlotLib 中的示例,它们似乎都已经从热图单元格值开始生成图像.

I looked through the examples in MatPlotLib and they all seem to already start with heatmap cell values to generate the image.

是否有一种方法可以将一堆不同的 x,y 转换为热图(其中 x,y 频率较高的区域会更暖")?

Is there a method that converts a bunch of x,y, all different, to a heatmap (where zones with higher frequency of x,y would be "warmer")?

推荐答案

如果你不想要六边形,可以使用 numpy 的 histogram2d 函数:

If you don't want hexagons, you can use numpy's histogram2d function:

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)

heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower')
plt.show()

这会生成一个 50x50 的热图.如果你想要,比如说,512x384,你可以把 bins=(512, 384) 放在对 histogram2d 的调用中.

This makes a 50x50 heatmap. If you want, say, 512x384, you can put bins=(512, 384) in the call to histogram2d.

示例:

这篇关于使用散点数据集在 MatPlotLib 中生成热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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