5

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

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 显示,即热图以相反的方向读取一个列表。我不确定为什么会这样。有什么建议么?

谢谢

4

2 回答 2

3

试试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')

产生:

在此处输入图像描述

于 2012-01-06T20:39:04.853 回答
0

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

ax2.imshow(heatmap.T, extent = extent,origin='lower')
于 2013-05-29T19:00:47.417 回答