繁体   English   中英

在colorbar ticks中修改科学记数法

[英]Modified scientific notation in colorbar ticks

我想在我的颜色栏中放置两个刻度。 matplotlib。

我的问题是颜色条上的数字格式。 我希望代替1.99e+00来获得1.9e0 更好的是如果数字将被舍入(例如2.0e0

数字0应保持为0

这是代码:

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

在此输入图像描述

您使用的格式可以简单地修改为小数后的一位数而不是两位,这自动包括舍入。 如果您将其作为文本修改进行操作,那么您想要的其他更改也很容易。

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

>>> format_yticklabel(1.99)
'2.0e0'

暂无
暂无

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

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