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

how to plot two-dimension array in python?

我有

的数据集
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 轴更改为数组的索引而不是数组的值?

你用什么模块绘图? Matplotlib?

你想要的是二维直方图。 有多种实现方式。我推荐 numpy 自带的那个 http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html

几件事:

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

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

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