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

查看:130
本文介绍了使用散点数据集在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天全站免登陆