简体   繁体   中英

Matplotlib colorbar ticks format when using scientific notation

I'm bothering you with a silly question, but I couldn't find an awnser. I'm trying to add a colorbar to an image using matplotlib. The issue comes when I try to format the ticks of the colorbar. I would like to have a scientific notation format, but at the same time control the number of decimals in the ticks. For example, with this code:

#format the colorbar ticks labels
sfmt=ticker.ScalarFormatter(useMathText=True) 
sfmt.set_powerlimits((0, 0))

cb1=fig1.colorbar(imp,
              cax=axcbar,
              orientation="vertical",
              ticks=np.linspace(1.03*np.min(imest),0.97*np.max(imest),4),
              format=sfmt)
cb1.set_label(r'$kg/s.m^2$')

I create this figure:

在此处输入图片说明

I would like to have in the ticks only one decimal (ie 5.210 --> 5.2). Is this possible in a simple way? I've tried a lot of things without success.

Thanks in advance.

You can change the tick's array from

ticks = np.linspace(1.03*np.min(imest),0.97*np.max(imest),4)

to something like:

ticks = np.arange(2.2, 6.2, 1) * 1e6

And the result would look like the plot: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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