Last active
October 27, 2017 12:33
-
-
Save AkiyukiOkayasu/cf6f762f8ddd79f79f149e13daf97675 to your computer and use it in GitHub Desktop.
グラフの最適な目盛りの求め方。 参考にさせていただいたサイト https://stackoverflow.com/questions/361681/algorithm-for-nice-grid-line-intervals-on-a-graph https://oshiete.goo.ne.jp/qa/2322056.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
def computeTickWidth(range, mostDivision): | |
minInterval = range / mostDivision | |
magnitude = pow(10, math.floor(math.log10(minInterval))) | |
residual = minInterval / magnitude | |
if residual > 5: | |
tickWidth = 5 * magnitude | |
elif residual > 2.5: | |
tickWidth = 2.5 * magnitude | |
elif residual > 2: | |
tickWidth = 2 * magnitude | |
else: | |
tickWidth = magnitude | |
return tickWidth | |
def computeAxisRange(min, max, tickWidth): | |
axisMax = 0.0 | |
axisMin = 0.0 | |
if min < 0 < max: | |
while abs(axisMax - max) > sys.float_info.epsilon and axisMax < max: | |
axisMax += tickWidth | |
while abs(axisMin - min) > sys.float_info.epsilon and axisMin > min: | |
axisMin -= tickWidth | |
else: | |
axisMin = min | |
axisMax = min | |
while abs(axisMax - max) > sys.float_info.epsilon and axisMax < max: | |
axisMax += tickWidth | |
return [axisMin, axisMax] | |
#------------------------------------------------------------------------ | |
# Example | |
# You can change MAX, MIN, MOSTDIV values. | |
MAX = 1.2 | |
MIN = -0.9 | |
RANGE = MAX - MIN | |
MOSTDIV = 4 | |
tick = computeTickWidth(RANGE, MOSTDIV) | |
minAxisValue, maxAxisValue = computeAxisRange(MIN, MAX, tick) | |
print("AxisMax: ", maxAxisValue) | |
print("AxisMin: ", minAxisValue) | |
print("TickWidth: ", tick) | |
#------------------------------------------------------------------------ | |
# AxisMax: 1.5 | |
# AxisMin: -1.0 | |
# TickWidth: 0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment