Luggi
Luggi

Reputation: 53

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. 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?

Upvotes: 5

Views: 1202

Answers (1)

fhdrsdg
fhdrsdg

Reputation: 10532

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

Upvotes: 4

Related Questions