vjg*_*vjg -1 python matlab numpy matplotlib octave
这是a的20 x 20像素图像zero
哪个图像存储在400的数组中:
X[0,:] = [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254
254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 246
232 206 206 246 254 255 255 255 255 255 255 255 255 255 255 255 255 255
246 182 132 132 132 161 206 232 254 255 255 255 255 255 255 255 255 255
255 254 206 132 132 132 132 132 132 182 246 255 255 255 255 255 255 255
255 255 255 254 206 132 132 161 182 161 132 161 232 255 255 255 255 255
255 255 255 255 255 254 182 132 161 232 246 182 132 161 232 255 255 255
255 255 255 255 255 255 255 246 182 132 182 246 246 182 132 182 246 255
255 255 255 255 255 255 255 255 255 246 182 132 182 254 232 161 132 206
254 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 206 132
161 232 254 255 255 255 255 255 255 255 255 255 255 246 182 132 161 206
161 132 182 246 255 255 255 255 255 255 255 255 255 255 255 254 206 132
132 132 132 132 206 254 255 255 255 255 255 255 255 255 255 255 255 255
232 182 161 132 132 182 232 254 255 255 255 255 255 255 255 255 255 255
255 255 254 246 232 206 206 232 254 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 254 254 254 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255]
Run Code Online (Sandbox Code Playgroud)
在Matlab中,我可以使用此图片displayData(X(0,:))命令查看此图片.
另外,使用以下命令:
% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:100);
displayData(X(sel, :));
Run Code Online (Sandbox Code Playgroud)
我可以显示不同的数字图片,如:
我尝试使用以下代码在Python中执行相同的操作:
import matplotlib.pyplot as plt
plt.imshow(X[0, :])
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是,它抛出了这个错误: Output: TypeError: Invalid dimensions for image data
numpy数组通常用于保存matplotlib中的绘图数据,因此最容易在灰度图像字节中读取到numpy数组,告诉你的numpy数组的形状
然后还有更多的情节设置
import numpy as np
import matplotlib.pyplot as plt
zstr = '255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 246 232 206 206 246 254 255 255 255 255 255 255 255 255 255 255 255 255 255 246 182 132 132 132 161 206 232 254 255 255 255 255 255 255 255 255 255 255 254 206 132 132 132 132 132 132 182 246 255 255 255 255 255 255 255 255 255 255 254 206 132 132 161 182 161 132 161 232 255 255 255 255 255 255 255 255 255 255 254 182 132 161 232 246 182 132 161 232 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 246 182 132 182 246 255 255 255 255 255 255 255 255 255 255 246 182 132 182 254 232 161 132 206 254 255 255 255 255 255 255 255 255 255 255 246 182 132 182 246 206 132 161 232 254 255 255 255 255 255 255 255 255 255 255 246 182 132 161 206 161 132 182 246 255 255 255 255 255 255 255 255 255 255 255 254 206 132 132 132 132 132 206 254 255 255 255 255 255 255 255 255 255 255 255 255 232 182 161 132 132 182 232 254 255 255 255 255 255 255 255 255 255 255 255 255 254 246 232 206 206 232 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 254 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255'
z, z.shape = np.array([int(i) for i in zstr.split(' ')]), (20,20)
fig, ax = plt.subplots()
ax.imshow(z, cmap=plt.cm.gray, interpolation='nearest')
# interpolation=None uses a smoother default interpolatiopn
plt.show()
Run Code Online (Sandbox Code Playgroud)