【问题标题】:Matplotlib plot numpy matrix as 0 indexMatplotlib 将 numpy 矩阵绘制为 0 索引
【发布时间】:2014-01-31 00:12:41
【问题描述】:

我准备了一个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()

剧情如下:

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

有没有简单的方法来做到这一点?

非常感谢!!

【问题讨论】:

  • 注意x轴和y轴在[-0.5, 9.5]范围内

标签: python numpy matrix matplotlib


【解决方案1】:

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

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

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

编辑:

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

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

输出:

【讨论】:

  • 谢谢!丹也是一样,这样做有什么好处,而不是使用范围??
【解决方案2】:

您可以将extent 可选参数用于plt.imshow() 函数,该函数记录在here 中。像这样:

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

对于一个任意形状的矩阵,你可以把这段代码改成这样:

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

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

【讨论】:

  • 谢谢!使用extent而不是下面Christian的方法有什么优势吗?
  • @GarethPrice:我想如果你有一个 1000x1000 的矩阵,我设置它的具体方式会更好,因为它让 numpy 选择应该如何间隔抽动。除此之外,它们几乎相同,并且不需要对 Christian 的方法进行太多修改即可使其也能像那样工作。
  • 我认为从 plot 命令控制这样的东西总是更可取的。另一种方法需要处理axis 对象并在show 等时注意。如果您事先知道您想要以某种方式使用轴,最好让它们正确开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2018-01-02
  • 1970-01-01
相关资源
最近更新 更多