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

后端 未结 2 465
轻奢々
轻奢々 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:47

    Running quiver doesn't necessarily return the type of mappable object that colorbar() requires. I think it might be because I explicitly "have colored the vector field based on another scalar quantity" like Heimdall says they did. Therefore, Hooked's answer didn't work for me.

    I had to create my own mappable for the color bar to read. I did this by using Normalize from matplotlib.colors on the data that I wanted to use to color my quiver vectors (which I'll call C, which is an array of the same shape as X, Y, U, and V.)

    My quiver call looks like this:

    import matplotlib.pyplot as pl
    import matplotlib.cm as cm
    import matplotlib.colors as mcolors
    import matplotlib.colorbar as mcolorbar
    pl.figure()
    
    nz = mcolors.Normalize()
    nz.autoscale(C)
    
    pl.quiver(X, Y, U, V, color=cm.jet(nz(C)))
    cax,_ = mcolorbar.make_axes(pl.gca())
    cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet, norm=nz)
    cb.set_label('color data meaning')
    

    Giving any other arguments to the colorbar function gave me a variety of errors.

    My example, where vector color shows third data axis

提交回复
热议问题