2

I see the following example to plot both image and colormap:

enter image description here

Code:

imgplot = plt.imshow(lum_img)
plt.colorbar()

From here: http://matplotlib.org/users/image_tutorial.html

But when I do this from my console, I get:

enter image description here

i.e. image displayed immediately, not waiting for second command, and after the second command the following error occurs:

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

user812786
  • 4,302
  • 5
  • 38
  • 50
Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

4

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.

enter image description here

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.

enter image description here

MB-F
  • 22,770
  • 4
  • 61
  • 116
  • How is it happen that in example the statements were entered apparently in separate lines? Also I am not able to run your second example in any way. Is it possible to run first example explicitly, without depending on line order? – Dims Feb 09 '16 at 18:26
  • The example is a bit sloppy in that regard. The commands are correct, but things don't work exactly like that when typing them in the command line. I have updated the answer with more examples. Although I don't know what you mean with "depending on line order", I hope you will find in these examples what you came looking for :) – MB-F Feb 09 '16 at 19:25