在Python中,plt imshow為啥對於np zeros和np ones矩陣都顯示為黑色影象

時間 2021-10-15 17:48:34

1樓:程軍

今天在使用pyplot.imshow函式顯示2D標量陣列時,不論陣列是255還是0,結果顯示出來的都是黑色。看了下官方文件

Normalize

, optional. The Normalize所以需要指定灰度範圍,有兩種方式可以正常顯示2D標量陣列:

pt.imshow(test_array, cmap="gray", vmin=0,vmax=255)

pt.imshow(test_array, cmap="gray", norm=colors.Normalize(0,255))

ps:加cmap="gray"是為了避免偽彩色import

numpy

asnp

from

matplotlib

import

pyplot

aspt

from

matplotlib

import

colors

if__name__

=="__main__"

:test_array=np

.ones

((255

,255

),dtype

=int)*

255pt

.figure(1

)pt.subplot

(211)pt

.imshow

(test_array

,cmap

="gray"

,vmin=0

,vmax

=255)pt

.xlabel

("1st way")pt

.subplot

(212)pt

.imshow

(test_array

,cmap

="gray"

,norm

=colors

.Normalize(0

,255

))pt

.xlabel

("2nd way")pt

.show()

在Python中,parameters與argument有什麼不同?

levi a parameter is a variable which we use in the function definition that is a handle that allows the code in the function to access the arguments f...

Python中yield的輸入錯誤理解在哪?

清都山水郎 推薦閱讀 Fluent Python 的第14章和第16章,該書原作者是巴西的Luciano Ramalho 以及 Effective Python 的第40條,該書原作者是美國的Brett Slatkin。這兩個部分詳細的講述了生成器以及 生成器如何進化成協程 引自安道吳珂所翻譯的Fl...

在python中,怎樣計算list的累積和?不能用loop或者library的function。

小小 def sum list list if len list 0 return 0 else return list 0 sum list list 1 greedisgood import numpy as np def cumsum list return np.array list cum...

在 Python 中,為什麼 type 類物件自身的型別是 type?

你開始接觸Python型別系統的一個非常有趣的分支了。Built in Functions 除了type obj 之外,type還有一種使用方法 type name,bases,dict 這兩個版本都會返回一個type型別的物件。單引數的版本會返回obj的型別 三引數的版本還是會返回一個type型別...

為什麼python中不建議在for迴圈中修改列表?

扁平結構比巢狀結構更好 Python之禪 比如 list map lambda x 4 if x 3 else x,a 如果一定要用for a 1,2,3,4,5,6 3變成4 4 if x 3 else x for x in a b 1,2,4,4,5,6 x for x in b if x 4 ...