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

发布于 2024-12-25 08:07:56 字数 396 浏览 1 评论 0原文

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

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 显示,即热图以相反的方式读取一个列表方向。我不知道为什么会发生这种情况。有什么建议吗?

谢谢

I am writing a script to make a heatmap for scatter data on two dimensionS. The following is a toy example of what I am trying to do:

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)

I should expect a the 'warmest' areas to be along y=x but instead they show up along y=-x+5 i.e the heatmap reads one list in the reverse direction. I am not sure why this is happening. Any suggestions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2025-01-01 08:07:56

尝试使用 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')

生成:

在此处输入图像描述

Try the imshow parameter origin=lower. By default it sets the (0,0) element of the array in the upper left corner.

For example:

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

Produces:

enter image description here

无边思念无边月 2025-01-01 08:07:56

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

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

Too keep the look of the heatmap consistent with what you see in the scatter, actually use:

ax2.imshow(heatmap.T, extent = extent,origin='lower')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文