How display one white pixel with mathplot imshow

前端 未结 1 1178
生来不讨喜
生来不讨喜 2021-01-28 07:18

I want to display one white pixel with mathplot:

import numpy as np
import matplotlib.pyplot as plt
plt.imshow([[0.99]], cmap=\'gray\', interpolation=\'nearest\'         


        
1条回答
  •  时光说笑
    2021-01-28 08:00

    The problem is that you only give imshow one value, so the colour scale is set around that value and it gets painted as the minimum value of the scale (thus black).

    Specify vmin and vmax, as shown here:

    import numpy as np
    import matplotlib.pyplot as plt
    plt.imshow([[0.99]], cmap='gray', interpolation='nearest', vmin=0, vmax=1)
    plt.show()
    

    More importantly, you need vmax, which will be mapped to white, to be the value you give imshow, and vmin to be smaller than that:

    import numpy as np
    import matplotlib.pyplot as plt
    
    max_value = np.random.random()
    min_value = -max_value # for instance
    
    plt.imshow([[max_value]], cmap='gray', interpolation='nearest',
               vmin=min_value, vmax=max_value)
    plt.show()
    

    0 讨论(0)
提交回复
热议问题