I'm trying to do a contourf plot of the divergence of a vector field with python and then add a colorbar to this plot. My levels are intended to be symmetric around zero from -0.01 to 0.01.
This is a part of my code:
div_levs = [-0.01, -0.005, -0.0025, 0.0025, 0.005, 0.01]
col = ['Blue', 'SteelBlue', 'White', 'Orange', 'Red']
c = plt.contourf(iwrf['x'], iwrf['y'], np.squeeze(iwrf['DIV'][ind_lev,:,:]),
levels=div_levs, colors=col, extend='both')
c.cmap.set_over('Magenta')
c.cmap.set_under('MidnightBlue')
bar = plt.colorbar(ticks=div_levs)
bar.set_label('1/s')
If I execute the python script it works, and everything gets drawn in the right way but the colormap is labeled with:
0.9900, 0.9950, 0.9975, 1.025, 1.0050, 1.0100
and on top of the colorbar a "-1" is displayed.
I've tried a lot, including setting the ticks of the colorbar after creating it, or setting the ticks in debug mode but nothing seems to change this behaviour.
Any ideas on this?
You can tell the colorbar's formatter to not use an offset by calling
bar.formatter.set_useOffset(False)
and then update the ticks with
bar.update_ticks()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With