gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:40:24 25 4
gpt4 key购买 nike

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

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

产生:

enter image description here

关于matplotlib - 使用散点数据集 python matplotlib 的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com