简体   繁体   English

Python colorbar ticks标记为+1的偏移量而不是指定的值

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

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. 我正在尝试用python做一个矢量场发散的contourf图,然后在这个图中添加一个颜色条。 My levels are intended to be symmetric around zero from -0.01 to 0.01. 我的水平旨在从-0.01到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: 如果我执行python脚本它可以工作,并且所有内容都以正确的方式绘制,但是colormap标记为:

0.9900, 0.9950, 0.9975, 1.025, 1.0050, 1.0100

and on top of the colorbar a "-1" is displayed. 并且在颜色条的顶部显示“-1”。

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 您可以通过调用告诉colorbar格式化程序不使用偏移量

bar.formatter.set_useOffset(False)

and then update the ticks with 然后用。更新刻度

bar.update_ticks()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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