matplotlib image shows in black and white, but I wanted gray
<p>I have a small code sample to plot images in matplotlib, and the image is shown as this :</p>

<p><a href="https://i.stack.imgur.com/r4CPR.png" rel="nofollow"><img src="https://i.stack.imgur.com/r4CPR.png" alt="enter image description here"></a></p>

<p>Notice the image in the black box has black background, while my desired output is this :</p>

<p><a href="https://i.stack.imgur.com/0jNcL.png" rel="nofollow"><img src="https://i.stack.imgur.com/0jNcL.png" alt="enter image description here"></a></p>

<p>My code to plot the image is this :</p>

<pre><code>plt.subplot(111)
plt.imshow(np.abs(img), cmap = 'gray')
plt.title('Level 0'), plt.xticks([]), plt.yticks([])
plt.show()
</code></pre>

<p>My understanding is that <code>cmap=grey</code> should display it in grayscale. Below is a snippet of the matrix <code>img</code> being plotted :</p>

<pre><code>[[ 192.77504036 +1.21392817e-11j  151.92357434 +1.21278246e-11j
   140.67585733 +6.71014111e-12j  167.76903747 +2.92050743e-12j
   147.59664180 +2.33718944e-12j   98.27986577 +3.56896094e-12j
    96.16252035 +5.31530804e-12j  112.39194666 +5.86689097e-12j....
</code></pre>

<p>What am I missing here ?</p>

<p>The problem seems to be that you have three channels while there should be only one, and that the data should be normalized between <code>[0, 1]</code>. I get a proper looking gray scaled image using this:</p>

<pre><code>import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread('Lenna.png')
# The formula below can be changed -- the point is that you go from 3 values to 1
imgplot = plt.imshow(np.dot(img[...,:3], [0.33, 0.33, 0.33]), cmap='gray')
plt.show()
</code></pre>

<p>This gives me:</p>

<p><a href="https://i.stack.imgur.com/PX6O5.png" rel="nofollow"><img src="https://i.stack.imgur.com/PX6O5.png" alt="enter image description here"></a></p>

<p>Also, a snapshot of the data:</p>

<pre><code>[[ 0.63152942  0.63152942  0.63800002 ...,  0.64705883  0.59658825 0.50341177]
 [ 0.63152942  0.63152942  0.63800002 ...,  0.64705883  0.59658825 0.50341177]
 [ 0.63152942  0.63152942  0.63800002 ...,  0.64705883  0.59658825 0.50341177]
 ...]
</code></pre>

python matplotlib