Python颜色条刻度标记为+1的偏移量,而不是指定的值 [英] Python colorbar ticks are labeled with an offset of +1 and not with specifed values

查看:63
本文介绍了Python颜色条刻度标记为+1的偏移量,而不是指定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python绘制矢量场的散度的contourf图,然后向该图添加一个颜色条.我的水平打算从-0.01到0.01在0附近对称.

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

如果我执行python脚本,它将起作用,并且所有内容都会在右侧绘制方式,但颜色图标记有:

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

在颜色栏顶部显示"-1".

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.

对此有何想法?

推荐答案

您可以告诉格式化程序不通过调用使用偏移量

You can tell the colorbar's formatter to not use an offset by calling

bar.formatter.set_useOffset(False)

然后用来更新刻度线

bar.update_ticks()

这篇关于Python颜色条刻度标记为+1的偏移量,而不是指定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆