在来源中,在image.py
, 在里面AxesImage
类(什么imshow
返回)一个方法_get_unsampled_image
在绘图过程中的某个时刻被调用。相关代码从第 226 行开始(matplotlib-1.5.3):
if A.dtype == np.uint8 and A.ndim == 3:
im = _image.frombyte(A[yslice, xslice, :], 0)
im.is_grayscale = False
else:
if self._rgbacache is None:
x = self.to_rgba(A, bytes=False)
# Avoid side effects: to_rgba can return its argument
# unchanged.
if np.may_share_memory(x, A):
x = x.copy()
# premultiply the colors
x[..., 0:3] *= x[..., 3:4]
x = (x * 255).astype(np.uint8)
self._rgbacache = x
所以输入的类型和大小A
得到检查:
if A.dtype == np.uint8 and A.ndim == 3:
在这种情况下没有预处理。否则,在不检查输入范围的情况下,您最终会乘以 255 并强制转换为uint8
:
x = (x * 255).astype(np.uint8)
我们知道如果x
是从 0 到 255 而不是 0 到 1:
In [1]: np.uint8(np.array([1,2,128,254,255])*255)
Out[1]: array([255, 254, 128, 2, 1], dtype=uint8)
所以光变暗了。我认为这颠倒了形象可能不是有计划的行为。
您可以比较以下值_rgbacache
在返回的对象中imshow
对于每个输入案例来观察结果,例如im._rbacache
where im = plt.imshow(np.float64(img))
.