使用散布数据集python matplotlib的热图

使用散布数据集python matplotlib的热图,matplotlib,heatmap,scatter-plot,Matplotlib,Heatmap,Scatter Plot,我正在写一个脚本,为二维上的分散数据制作热图。以下是我正在尝试做的一个玩具示例: import numpy as np from matplotlib.pyplot import* x = [1,2,3,4,5] y = [1,2,3,4,5] heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] imshow(heatmap,

我正在写一个脚本,为二维上的分散数据制作热图。以下是我正在尝试做的一个玩具示例:

import numpy as np
from matplotlib.pyplot import*
x = [1,2,3,4,5]
y = [1,2,3,4,5]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
imshow(heatmap, extent = extent)
我认为“最热”的区域应该是沿y=x的方向,但它们却沿y=-x+5显示,即热图显示的是一个相反方向的列表。我不知道为什么会这样。有什么建议吗


谢谢

尝试
imshow
参数
origin=lower
。默认情况下,它在左上角设置数组的(0,0)元素

例如:

import numpy as np
import matplotlib.pyplot as plt
x = [1,2,3,4,5,5]
y = [1,2,3,4,5,5]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=10)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.imshow(heatmap, extent = extent)
ax1.set_title("imshow Default");
ax2 = fig.add_subplot(212)
ax2.imshow(heatmap, extent = extent,origin='lower')
ax2.set_title("imshow origin='lower'");

fig.savefig('heatmap.png')
产生:


也要保持热图的外观与您在散点图中看到的一致,实际使用:

ax2.imshow(heatmap.T, extent = extent,origin='lower')