2012-04-25 11 views
1

私はカラーバーに2つのティックを配置したいと思います。 matplotlib。カラーバーティックで修正された科学記法

私の問題は、カラーバーの数字の形式にあります。私は1.99e+00の代わりに1.9e0を持っています。数値が丸められた場合(たとえば、2.0e0

数値が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() 

enter image description here

+0

上記の例のプロットはどこで取得できましたか? – Yann

答えて

3

あなたが使用しているフォーマットは自明自動的に丸め含ま小数点の代わりに、2、後1桁ように修正することができます:ここ

はコードです。テキストの変更として行う場合、他の変更も簡単です。

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

>>> format_yticklabel(1.99) 
'2.0e0' 
関連する問題