matplotlib绘制小图像而无需重新采样

use*_*241 6 python matplotlib

我正在尝试使用matplotlib在python中绘制一个小图像,并希望显示的轴具有与它生成的numpy数组相同的形状,即数据不应重新采样.换句话说,数组中的每个条目应对应于屏幕上的像素(或其左侧).这看起来微不足道,但即使在网上拖网一段时间之后,我似乎无法让它起作用:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = np.random.rand(30,40)

fig = plt.figure()
fig.add_axes(aspect="equal",extent=[0, X.shape[1], 0, X.shape[0]])
ax = fig.gca()
ax.autoscale_view(True, False, False)
ax.imshow(X, cmap = cm.gray)

plt.show()
Run Code Online (Sandbox Code Playgroud)

tim*_*day 5

我自己也有同样的问题.如果interpolation='nearest'选项imshow不够好,那么如果你的主要目标是在matplotlib中看到原始的,未缩放的,非插值的,未扫描的像素,那么你就无法击败figimage恕我直言.演示:

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

a=256*np.random.rand(64,64)

f0=plt.figure()
plt.imshow(a,cmap=plt.gray())
plt.suptitle("imshow")

f1=plt.figure()
plt.figimage(a,cmap=plt.gray())
plt.suptitle("figimage")

plt.show()
Run Code Online (Sandbox Code Playgroud)

当然这意味着放弃轴(或以某种方式自己绘制).有一些选项可以figimage让你围绕图形移动图像,所以我想可以在其他方法创建的某些轴上操纵它们.

  • 如果有人想知道,除了 Ipython Notebook 中的图像之外,我能够让 Figimage 工作的唯一方法是在 plt.show() 之前添加一组空的(“关闭”)轴,类似于此 S/O 帖子:http://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces。否则,它只是没有策划任何事情。 (2认同)