This happens because you run both commands separately.
In the first command the image is created and displayed inline. Then the figure object is discarded and cannot be changed anymore.
The second command now applies to a new figure, that does not contain an image.
There are several possible solutions:
Example 1: normal mode
This will show the figure in a separate window. All operations apply to the same figure, which remains invisible until displayed with plt.show()
. This function then blocks the script until the figure is closed.
In [1]: import matplotlib.pyplot as plt
In [2]: import matplotlib.image as mpimg
In [3]: img = mpimg.imread('/tmp/stinkbug.png')
In [4]: lum_img = img[:, :, 0]
In [5]: plt.imshow(lum_img)
Out[5]: <matplotlib.image.AxesImage at 0x7f1a24057748>
In [6]: plt.colorbar()
Out[6]: <matplotlib.colorbar.Colorbar at 0x7f1a24030a58>
In [7]: plt.show()
Example 2: interactive mode
This is the same as example 1, but the figure window is shown immediately and updated with successive plotting calls. (For me this works in IPython but I only get a black window in Jupyter QtConsole.)
In [1]: import matplotlib.pyplot as plt
In [2]: import matplotlib.image as mpimg
In [3]: plt.ion()
In [4]: img = mpimg.imread('/tmp/stinkbug.png')
In [5]: lum_img = img[:, :, 0]
In [6]: plt.imshow(lum_img)
Out[6]: <matplotlib.image.AxesImage at 0x7f7f2061e9b0>
In [7]: plt.colorbar()
Out[7]: <matplotlib.colorbar.Colorbar at 0x7f7f20605128>
Example 3: inline plotting
If you want inline mode, you can simply perform multiple commands in one input line, like this.

Example 4: advanced inline plotting
Manually create a figure object. Perform operations on this object (create subplot, draw image, add colorbar) and display the inline figure anytime by simply typing its name in the command line.
