简体   繁体   中英

Custom colorbar with quiver plot in matplotlib

I'm trying to produce a quiver plot, vector lengths range from 0. to 15., and I'd like to use a grey colormap but starting from, say, half-range, such that 0. is already grey and 15. is black. What I've done so far is:

cmap = cm.get_cmap('Greys', 10)

norm = matplotlib.colors.Normalize(vmin=-5.,vmax=15.,clip=False)
Q = ax.quiver(xi, yi, zix, ziy, lengths * 1000., units='inches', width=0.008, headwidth=6, headlength=7, scale=5,
              scale_units='inches',cmap=cmap, norm=norm)
cb = plt.colorbar(Q, cax=ax3, ticks=[0.0, 3.0, 6.0, 9.0, 12.0, 15.0], format='%.1f', norm=norm)

The color range is correct but the whole colormap is shown in the colorbar, ie starting from the white color. What am I missing?

The "Greys" colormap starts at white and goes to black. Due to your normalization -5 is white and 15 is black.

What you seem to really want is a normalization of vmin=0,vmax=15. and a colormap which starts with a grey color already:

import matplotlib.colors
norm = matplotlib.colors.Normalize(vmin=0,vmax=15.,clip=False)
cmap = matplotlib.colors.ListedColormap(plt.cm.Greys(np.linspace(0.25,1,10)), "name")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM