Python Matplotlib 将 numpy 矩阵绘制为 0 索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21071128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:59:18  来源:igfitidea点击:

Matplotlib plot numpy matrix as 0 index

pythonnumpymatrixmatplotlib

提问by GarethPrice

I prepare a numpy matrix then use matplotlib to plot the matrix, such as:

我准备了一个 numpy 矩阵,然后使用 matplotlib 绘制矩阵,例如:

>>> import numpy
>>> import matplotlib.pylab as plt
>>> m = [[0.0, 1.47, 2.43, 3.44, 1.08, 2.83, 1.08, 2.13, 2.11, 3.7], [1.47, 0.0, 1.5,     2.39, 2.11, 2.4, 2.11, 1.1, 1.1, 3.21], [2.43, 1.5, 0.0, 1.22, 2.69, 1.33, 3.39, 2.15, 2.12, 1.87], [3.44, 2.39, 1.22, 0.0, 3.45, 2.22, 4.34, 2.54, 3.04, 2.28], [1.08, 2.11, 2.69, 3.45, 0.0, 3.13, 1.76, 2.46, 3.02, 3.85], [2.83, 2.4, 1.33, 2.22, 3.13, 0.0, 3.83, 3.32, 2.73, 0.95], [1.08, 2.11, 3.39, 4.34, 1.76, 3.83, 0.0, 2.47, 2.44, 4.74], [2.13, 1.1, 2.15, 2.54, 2.46, 3.32, 2.47, 0.0, 1.78, 4.01], [2.11, 1.1, 2.12, 3.04, 3.02, 2.73, 2.44, 1.78, 0.0, 3.57], [3.7, 3.21, 1.87, 2.28, 3.85, 0.95, 4.74, 4.01, 3.57, 0.0]]
>>> matrix = numpy.matrix(m)
>>> matrix
matrix([
    [ 0.  ,  1.47,  2.43,  3.44,  1.08,  2.83,  1.08,  2.13,  2.11, 3.7 ],
    [ 1.47,  0.  ,  1.5 ,  2.39,  2.11,  2.4 ,  2.11,  1.1 ,  1.1 , 3.21],
    [ 2.43,  1.5 ,  0.  ,  1.22,  2.69,  1.33,  3.39,  2.15,  2.12, 1.87],
    [ 3.44,  2.39,  1.22,  0.  ,  3.45,  2.22,  4.34,  2.54,  3.04, 2.28],
    [ 1.08,  2.11,  2.69,  3.45,  0.  ,  3.13,  1.76,  2.46,  3.02, 3.85],
    [ 2.83,  2.4 ,  1.33,  2.22,  3.13,  0.  ,  3.83,  3.32,  2.73, 0.95],
    [ 1.08,  2.11,  3.39,  4.34,  1.76,  3.83,  0.  ,  2.47,  2.44, 4.74],
    [ 2.13,  1.1 ,  2.15,  2.54,  2.46,  3.32,  2.47,  0.  ,  1.78, 4.01],
    [ 2.11,  1.1 ,  2.12,  3.04,  3.02,  2.73,  2.44,  1.78,  0.  , 3.57],
    [ 3.7 ,  3.21,  1.87,  2.28,  3.85,  0.95,  4.74,  4.01,  3.57, 0.  ]
])
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1,1,1)
>>> ax.set_aspect('equal')
>>> plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean)
>>> plt.colorbar()
>>> plt.show()

The plot shows like this:

情节显示如下:

enter image description here

enter image description here

This is fine, except for the fact that I would like my axes to go from 1-10, rather than 0-9 (derived from python's 0 indexing)

这很好,除了我希望我的轴从 1-10 而不是 0-9(派生自 python 的 0 索引)

Is there a simple way to do this?

有没有一种简单的方法可以做到这一点?

Many thanks!!

非常感谢!!

采纳答案by Dan

You can use the extentoptional parameter to the plt.imshow()function, which is documented here. Like this:

您可以使用该函数的extent可选参数plt.imshow()此处记录该参数。像这样:

#All the stuff earlier in the program
plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean, extent=(0.5,10.5,0.5,10.5))
plt.colorbar()
plt.show()

For a matrix with an arbitrary shape, you could change this code to something like this:

对于具有任意形状的矩阵,您可以将此代码更改为如下所示:

#All the stuff earlier in the program
plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean,
    extent=(0.5,numpy.shape(matrix)[0]+0.5,0.5,numpy.shape(matrix)[1]+0.5))
plt.colorbar()
plt.show()

This produces a plot that looks like this:

这会产生一个如下所示的图:

Plot output

Plot output

回答by Christian

To get the desired output add these lines after your code, but beforeplt.show():

要获得所需的输出,请在代码之后但之前添加这些行plt.show()

...
labels = [0, 1, 3, 5, 7, 9]
ax.set_xticklabels(labels)
plt.show()

Note that x-axis and y-axis are in the range [-0.5, 9.5]not int [0, 9]

请注意,x 轴和 y 轴[-0.5, 9.5]不在 int的范围内[0, 9]

Edit:

编辑:

To do it in a more flexible way (in fact, another way of the showed above):

要以更灵活的方式进行(实际上是上面显示的另一种方式):

labels = range(0, len(m[0]))
plt.xticks(labels)
plt.show()

Output:

输出:

enter image description here

enter image description here