Matplotlib : quiver and imshow superimposed, how can I set two colorbars?


Question

I have a figure that consists of an image displayed by imshow(), a contour and a vector field set by quiver(). I have colored the vector field based on another scalar quantity. On the right of my figure, I have made a colorbar(). This colorbar() represents the values displayed by imshow() (which can be positive and negative in my case). I'd like to know how I could setup another colorbar which would be based on the values of the scalar quantity upon which the color of the vectors is based. Does anyone know how to do that?

Here is an example of the image I've been able to make. Notice that the colors of the vectors go from blue to red. According to the current colorbar, blue means negative. However I know that the quantity represented by the color of the vector is always positive.

enter image description here

1
9
8/15/2012 3:13:17 PM

Simply call colorbar twice, right after each plotting call. Pylab will create a new colorbar matching to the latest plot. Note that, as in your example, the quiver values range from 0,1 while the imshow takes negative values. For clarity (not shown in this example), I would use different colormaps to distinguish the two types of plots.

import numpy as np
import pylab as plt

# Create some sample data
dx = np.linspace(0,1,20)
X,Y = np.meshgrid(dx,dx)
Z  = X**2 - Y
Z2 = X

plt.imshow(Z)
plt.colorbar()

plt.quiver(X,Y,Z2,width=.01,linewidth=1)
plt.colorbar() 

plt.show()

enter image description here

7
8/15/2012 3:23:59 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon