Matplot图像被Python压缩

时间:2019-06-02 13:39:18

标签: python matlab

我正在尝试将matlab代码转换为python。这是我写的python代码

import numpy as np
import numpy.matlib as nm
import matplotlib.pyplot as plt
N = 1000;     # time points
M =   20;     # channels
nTrials = 50; # number of trials
t = np.linspace(0,6*np.pi,N);
img=(nm.repmat(np.sin(t),M,1))
plt.imshow(img);
plt.colorbar()
plt.show()

输出如下

python output

MATLAB代码

N = 1000;     % time points
M =   20;     % channels
nTrials = 50; % number of trials

% time vector (radian units)
t = linspace(0,6*pi,N);
img=repmat( sin(t),M,1 )
imagesc(img)

matlab中的输出如下 matlab output

或者,我尝试对简单的样本数据执行相同的操作 MATLAB

C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(C)
colorbar

PYTHON

C =np.array([0, 2 ,4 ,6, 8, 10, 12, 14, 16, 18, 20, 22])
C=np.reshape(C,(3,4))
plt.imshow(C);
plt.colorbar()
plt.show()

两者的图像输出相同 sample data image

我不明白为什么在罪恶浪中图像会被压缩

1 个答案:

答案 0 :(得分:1)

Matlab自动缩放y轴以与x匹配。这就是为什么即使x范围比y范围大50倍,仍会看到接近正方形的图像的原因。

要在matplotlib上获得类似效果,请尝试将y范围设置为与x类似,例如设置M = 800。 enter image description here

或者,如果您不想更改M,仍然可以使用matplotlib缩放y范围:

plt.imshow(img)
plt.axis('tight')
plt.colorbar()
plt.show()

enter image description here 注意,在这种情况下,y轴的范围保持不变。