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

后端 未结 2 501
轻奢々
轻奢々 2021-02-03 12:10

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

2条回答
  •  悲哀的现实
    2021-02-03 12:50

    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

提交回复
热议问题