【发布时间】:2019-04-27 11:13:06
【问题描述】:
我有一个由随机值组成的矩阵,我用ax.matplot 绘制了它,其中每个矩阵单元格的值都绘制在矩阵单元格的中心,我需要增加绘图的大小(特别是宽度)并将颜色图(cmap)设置为“透明”,其中矩阵图的单元格将为white,单元格中心的矩阵值为black。
代码:
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
print (matrix)
fig, ax = plt.subplots()
ax.matshow(matriz, cmap=None,interpolation='nearest')
for i in xrange(20):
for j in xrange(20):
number = matrix[j,i]
ax.text(i, j, str(number), va='center', ha='center')
plt.show()
编辑
就是代码中的matrix(https://i.stack.imgur.com/e3mGO.png(matrix),用命令matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))生成的矩阵,图片来自控制台。
我尝试更改fig,ax = plt.subplot() 中的figsize,但没有任何改变。我也设置了cmap = none,但什么也没发生。
另一个问题是,在第一张图中,x和y轴的索引中的数字从0到15不等,步进5,比如0、5、10、15(我'已将其标记为红色)。但在第二张图片中(也用红色标记),值是从 0 到 19,没有步长。如果可能的话,我需要得到第二张图片的结果,索引从 0 到 19 不等,没有步骤。
图片 1:我得到了什么: https://i.stack.imgur.com/mlExQ.png
图2:我需要得到的东西:https://i.stack.imgur.com/T3CC4.png
【问题讨论】:
-
您的代码中的
matrix是什么?如果你使用 Python 2.7,你应该相应地标记问题。 -
我已经编辑过了。
-
我希望 this example 能有所帮助。
标签: python-2.7 matplotlib axes colormap