我有一组 X,Y 数据点(大约 10k),很容易绘制为散点图,但我想将其表示为热图。
我查看了 Matplotlib 中的示例,它们似乎都已经从热图单元格值开始生成图像。
有没有一种方法可以将一堆不同的 x、y 转换为热图(其中 x、y 频率较高的区域会“更温暖”)?
如果你不需要六边形,你可以使用numpy的histogram2d
功能:
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
.
Example:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)