繁体   English   中英

plt.imshow 无法正常工作

[英]plt.imshow is not working as it should be

所以我在python中有这个代码

import numpy as np
import matplotlib.pyplot as plt
M = np.zeros((490,1900))
fig, ax = plt.subplots()
plt.imshow(M, aspect='auto')
ax.set_ylabel('f2')
ax.set_xlabel('f1')
plt.xlim((0, 490))
plt.ylim((0, 1900))
x_range = np.arange(0, 490, step=50)
x_strings = [str(x+190) for x in x_range]
plt.xticks(x_range, x_strings)
y_range = np.arange(0, 1900, step=200)
y_strings = [str(y+1710) for y in y_range]
plt.yticks(y_range, y_strings)
plt.colorbar()
plt.ioff()
plt.show()

当我编译它时,它应该用我创建的零 (M) 网格覆盖整个区域,但它覆盖了它的一部分,如下所示

图片

我在这里做错了什么?

我认为您在创建矩阵时只是让自己感到困惑。

当您可能打算创建一个具有 1900 行和 490 列的矩阵时,您正在创建一个具有 490 行和 1900 列的矩阵。

只需更改定义矩阵M的顺序即可解决您的问题:

import numpy as np
import matplotlib.pyplot as plt
M = np.zeros((1900, 490)) # change is here
fig, ax = plt.subplots()
plt.imshow(M, aspect='auto')
ax.set_ylabel('f2')
ax.set_xlabel('f1')
plt.xlim((0, 490))
plt.ylim((0, 1900))
x_range = np.arange(0, 490, step=50)
x_strings = [str(x+190) for x in x_range]
plt.xticks(x_range, x_strings)
y_range = np.arange(0, 1900, step=200)
y_strings = [str(y+1710) for y in y_range]
plt.yticks(y_range, y_strings)
plt.colorbar()
plt.ioff()
plt.show()

其中产生:

在此处输入图片说明


您当然也可以通过反转xlimylim来更改绘图限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM