gpt4 book ai didi

python - 如何在Python中绘制二维数组?

转载 作者:行者123 更新时间:2023-11-30 23:13:29 26 4
gpt4 key购买 nike

我有数据集

data = [[1,2,3], [4,5,6], [7,8,9]].

并调用

plot(data)
plot.show()

然后 y 轴被视为内部数组的值。

我想要的是

f(0,0) = 1, f(0,1) = 2, f(1,2) = 3, 
f(1,0) = 4, f(1,1) = 5, f(1,2) = 6,
f(2,0) = 7, f(2,1) = 8, f(2,2) = 9

问题是,如何将 y 轴更改为数组的索引而不是数组的值?

最佳答案

一些事情:

1) Python 没有 2D、f[i,j]、索引表示法,但要获得它,您可以使用 numpy。从示例中选择任意索引对:

import numpy as np
f = np.array(data)
print f[1,2]
# 6
print data[1][2]
# 6

2)然后对于情节你可以这样做:

plt.imshow(f, interpolation="nearest", origin="upper")
plt.colorbar()
plt.show()

enter image description here

因此,这里有代表性的颜色,其中 f 数组中有数字。

这里我指定了origin="upper"。通常人们希望数据数组(而不是图像)底部的 (0,0) 点,但你用 (0,0) 写出数组> 在左上角,这就是 "upper" 的作用。顺便说一句,这也是默认设置,但明确使用它可能会表明存在一个选项。

关于python - 如何在Python中绘制二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292104/

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