Python colorbar ticks are labeled with an offset of +1 and not with specifed values

梦想与她 提交于 2021-02-08 15:27:11

问题


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?


回答1:


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()


来源:https://stackoverflow.com/questions/29280006/python-colorbar-ticks-are-labeled-with-an-offset-of-1-and-not-with-specifed-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!