2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-01-11
動作環境
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

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

結果

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の時の処理が失敗する

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

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

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

結果

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

関連

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?