Python3 > Y軸の最大値に対するtick間隔を計算 > v0.1, v0.2

```txt:動作環境
ideone : Python 3.5
```

### 内容

- グラフのY軸のtickを計算する
- 与えられたY軸の最大値を元に計算
    - 100以下 => 10
    - 100超過 => 20
    - 200超過 => 50
    - 500超過 => 100
    - 2019-01-11時点のMicrosoft Excel (Office 365)と同じ?
    - 2019-01-11時点のGoogleスプレッドシートとは異なる
- 最大値が1, 10, 2000なども同様に計算
- C++ Builderで使うが、Pythonで計算だけ実装してみた

### 実装 v0.1

https://ideone.com/eZfEMS

```py
'''
v0.1 Jan. 11, 2019
    - add Test_main()
    - add calcExponent()
    - add calcYtick()
'''


def calcExponent(yval, amin, amax):
    ywrk = yval
    pwr = 0
    if ywrk > amax:
        while (ywrk > amax):
            ywrk /= 10
            pwr += 1
    elif ywrk < amin:
        while (ywrk < amin):
            ywrk *= 10
            pwr -= 1
    return pwr


def calcYtick(ymax):
    # <=100 : 10
    # 100>  : 20
    # 200> : 50
    # 500> : 100

    thrs = [100, 200, 500]  # Threashold

    # 1. find the exponent
    pwr = calcExponent(ymax, amin=100, amax=1000)
    
    # 2. display the value accordingly
    if ymax <= thrs[0] * 10**pwr:
    	res = 10 * 10**pwr
    elif ymax < thrs[1] * 10**pwr:
    	res = 20 * 10**pwr
    elif ymax < thrs[2] * 10**pwr:
    	res = 50 * 10**pwr
    else:
	    res = 100 * 10**pwr
    print(str(ymax) + "=>" + str(res), end=':')


def Test_main():
    alist = [100, 101, 201, 501, 2001, 5001, 20001, 1, 1.1, 2]
    ress = [10, 20, 50, 100, 500, 1000, 5000, 0.1, 0.2, 0.5]
    for elem,ares in zip(alist, ress):
        calcYtick(elem)
        print(ares)

Test_main()
```

### 結果

```txt
100=>10:10
101=>20:20
201=>50:50
501=>100:100
2001=>500:500
5001=>1000:1000
20001=>5000:5000
1=>0.1:0.1
1.1=>0.2:0.2
2=>0.5:0.5
```

### 不具合

<font color=red>**不具合**</font>

- 最大値が0の時の処理が失敗する


### 実装 v0.2 > ymax=0のとき、ytick=0とする

- 変更点
    - ymax=0のとき、ytickを0とする

https://ideone.com/1tThCj

```py
import sys


'''
v0.2 Jan. 11, 2019
    - handle [Ymax=0] case
v0.1 Jan. 11, 2019
    - add Test_main()
    - add calcExponent()
    - add calcYtick()
'''


def calcExponent(yval, amin, amax):
    ywrk = yval
    pwr = 0
    if ywrk > amax:
        while (ywrk > amax):
            ywrk /= 10
            pwr += 1
    elif ywrk < amin:
        while (ywrk < amin):
            ywrk *= 10
            pwr -= 1
    return pwr


def calcYtick(ymax):
    # <=100 : 10
    # 100>  : 20
    # 200> : 50
    # 500> : 100

    if abs(ymax - 0.0) < sys.float_info.epsilon:
        res = 0.0
        print(str(ymax) + "=>" + str(res), end=':')
        return res

    thrs = [100, 200, 500]  # Threashold

    # 1. find the exponent
    pwr = calcExponent(ymax, amin=100, amax=1000)
    
    # 2. display the value accordingly
    if ymax <= thrs[0] * 10**pwr:
    	res = 10 * 10**pwr
    elif ymax < thrs[1] * 10**pwr:
    	res = 20 * 10**pwr
    elif ymax < thrs[2] * 10**pwr:
    	res = 50 * 10**pwr
    else:
	    res = 100 * 10**pwr
    print(str(ymax) + "=>" + str(res), end=':')


def Test_main():
    alist = [100, 101, 201, 501, 2001, 5001, 20001, 1, 1.1, 2, 0.0]
    ress = [10, 20, 50, 100, 500, 1000, 5000, 0.1, 0.2, 0.5, 0.0]
    for elem,ares in zip(alist, ress):
        calcYtick(elem)
        print(ares)

Test_main()

```

結果

```txt
100=>10:10
101=>20:20
201=>50:50
501=>100:100
2001=>500:500
5001=>1000:1000
20001=>5000:5000
1=>0.1:0.1
1.1=>0.2:0.2
2=>0.5:0.5
0.0=>0.0:0.0
```

### 関連

- [Python > 2つのfloat値の比較 > sys.float_info.epsilonを使う](https://qiita.com/7of9/items/ab76738263f6a0f6b8da)