Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve colorbar instance from figure in matplotlib

all. I want to update the colorbar of a figure when the imagedata is changed. So something like:

img = misc.lena() fig = plt.figure() ax = plt.imshow(im) plt.colorbar(ax) newimg = img+10*np.randn(512,512)  def update_colorbar(fig,ax,newimg):     cbar = fig.axes[1]     ax.set_data(newimg)     cbar.update_normal(ax)     plt.draw() 

but it seems that returned results from fig.axes() does not have the colorbar instance like I expected. I can probably just pass the colorbar instance as an argument to the update function, but I thought just passing one fig parameter may be good enough. Can anyone explain a little bit on how to retrieve the colorbar from the figure? Or why 'fig.axes()' doesn't return the AxesImage or Colobar instance but just the Axes or AxesSubplot? I think I just need more understanding of the Axes/Figure stuff.Thank you!

like image 577
Wins Avatar asked Nov 06 '13 16:11

Wins


People also ask

What is a mappable Matplotlib?

A colorbar needs a "mappable" ( matplotlib. cm. ScalarMappable ) object (typically, an image) which indicates the colormap and the norm to be used. In order to create a colorbar without an attached image, one can instead use a ScalarMappable with no associated data.

How do you normalize a Colorbar in Python?

norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);

What does PLT axis () do?

axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt. plot(x, np.


2 Answers

Sometimes it can be useful to retrieve a colorbar even if it was not held in a variable.

In this case, it is possible to retrieve the colorbar from the plot with:

# Create an example image and colourbar img = np.arange(20).reshape(5,4) plt.imshow(img) plt.colorbar()  # Get the current axis  ax = plt.gca()          # Get the images on an axis im = ax.images          # Assume colorbar was plotted last one plotted last cb = im[-1].colorbar     # Do any actions on the colorbar object (e.g. remove it) cb.remove() 

EDIT:

or, equivalently, the one liner:

plt.gca().images[-1].colorbar.remove() 

N.B.: see also comments for the use of ax.collections[-1] instead of ax.images[-1]. For me it always worked only the first way, I don't know what depends on, maybe the type of data or plot.


Now you can operate on cb as if it were stored using commands described in the colorbar API. For instance you could change xlim or call update as explained in other comments. You could remove it with cb.remove() and recreate it with plt.colorbar().

plt.draw() or show should be called after to update plot.

As the image is the mappable associated to the colorbar and can be obtained with cb.mappable.

like image 177
Vincenzooo Avatar answered Sep 17 '22 18:09

Vincenzooo


First off, I think you're getting a bit confused between the axes (basically, the plot), the figure, the scalar mappable (the image, in this case), and the colorbar instance.

The figure is the window that the plot is in. It's the top-level container.

Each figure usually has one or more axes. These are the plots/subplots.

Colorbars are also inside the figure. Adding a colorbar creates a new axes (unless you specify otherwise) for the colorbar to be displayed in. (It can't normally be displayed in the same axes as the image, because the colorbar needs to have its own x and y limits, etc.)

Some of your confusion is due to the fact that you're mixing the state-machine interface and the OO interface. It's fine to do this, but you need to understand the OO interface.

fig.axes[1] isn't the colorbar instance. It's the axes that the colorbar is plotted in. (Also, fig.axes[1] is just the second axes in the figure. It happens to be the axes that the colorbar is in for a figure with one subplot and one colorbar, but that won't generally be the case.)

If you want to update the colorbar, you'll need to hold on to the colorbar instance that colorbar returns.

Here's an example of how you'd normally approach things:

import matplotlib.pyplot as plt import numpy as np  data = np.random.random((10,10)) # Generate some random data to plot  fig, ax = plt.subplots() # Create a figure with a single axes. im = ax.imshow(data)     # Display the image data cbar = fig.colorbar(im)  # Add a colorbar to the figure based on the image 

If you're going to use update_normal to update the colorbar, it expects a ScalarMappable (e.g. an image created by imshow, the collection that scatter creates, the ContourSet that contour creates, etc) to be passed in. (There are other ways to do it, as well. Often you just want to update the limits, rather than the whole thing.) In the case of the code above, you'd call cbar.update_normal(im).

However, you haven't created a new AxesImage, you've just changed it's data. Therefore, you probably just want to do:

cbar.set_clim(newimg.min(), newimg.max()) 
like image 44
Joe Kington Avatar answered Sep 18 '22 18:09

Joe Kington