简体   繁体   中英

Modified scientific notation in colorbar ticks

i would like to place two ticks in my colorbar. matplotlib.

my problem is at the format of the numbers at the colorbar. I would like instead of 1.99e+00 to have 1.9e0 . Better would be if the number would be rounded (eg 2.0e0 )

The number 0 should stay as 0 .

here is the code:

from scipy import *
import matplotlib.pylab as plt
import numpy as np

def main():
# Creating the grid of coordinates x,y 
x,y = ogrid[-1.:1.:.01, -1.:1.:.01]

z = 3*y*(3*x**2-y**2)/4 + .5*cos(6*pi * sqrt(x**2 +y**2) + arctan2(x,y))

fig = plt.figure()

ax = fig.add_subplot(111)
result = ax.imshow(z, 
                   origin='lower', 
                   extent=[0,2,0,400],              
           interpolation='nearest',
           aspect='auto'
          )
cbar = fig.colorbar( result , ticks=[ 0 , z.max() ])
    cbar.ax.set_yticklabels([ 0 ,  '{0:.2e}'.format( z.max()) ]) 
    cbar.outline.remove()

plt.show()

if __name__ == '__main__':
    main()

在此输入图像描述

The format you're using can be trivially modified to be one digit after the decimal instead of two, which automatically includes rounding. The other changes you want are easy too if you do them as text modifications.

def format_yticklabel(number):
    return '{0:.1e}'.format(number).replace('+0', '').replace('-0', '-')

>>> format_yticklabel(1.99)
'2.0e0'

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