Ticking函数grapher

时间:2008-12-06 21:40:28

标签: python algorithm math

我想弄清楚以下问题。 我正在构建另一个数学函数grapher,该函数是在其预定义的x,y范围上绘制的,这一切都很好。

现在我正在研究X,Y轴的背景和滴答(如果显示任何轴)。

我制定了以下内容。 我的固定宽度为250 p 滴答间隙应介于12.5和50p之间。

刻度应指示单位或半单位范围,我的意思是以下。

x range(-5,5):one tick = 1

x range(-1,1):一个tick = 0.5或0.1,具体取决于每个选项将产生的间隙。

x范围(0.1,0.3):0.05

鉴于Xrange 你如何得到整数或半单位范围之间的滴答数?

或许还有其他方法可以解决这类问题。

5 个答案:

答案 0 :(得分:4)

这样做的一种方法是“规范化”最小值和最大值之间的差异,并对该值进行区分。在python:

delta = maximum - minimum
factor = 10**math.ceil(math.log(delta,10))  # smallest power of 10 greater than delta
normalised_delta = delta / factor           # 0.1 <= normalised_delta < 1
if normalised_delta/5 >= 0.1:
  step_size = 0.1
elif normalised_delta/5 >= 0.05:
  step_size = 0.05
elif normalised_delta/20 <= 0.01:
  step_size = 0.01
step_size = step_size * factor

上面的代码假设您想要最大的差距。对于最小的,如果符合以下条件,则使用以下内容:

if normalised_delta/20 == 0.005:
  step_size = 0.005
elif normalised_delta/20 <= 0.01:
  step_size = 0.01
elif normalised_delta/5 >= 0.05:
  step_size = 0.05

除了存在多个合适值的可能性之外,还有一些令人担忧的可能性,即没有。例如,范围[0,24],其中12.5p的间隙将给出1.2的步长,并且50p的间隙将给出步长4.8。中间没有“单位”或“半单位”。问题是12.5p和50p之间的差异是因子4,而0.01和0.05之间的差异是因子5.所以你必须稍微扩大允许的间隙范围并相应地调整代码

澄清一些幻数:20和5的除法分别对应于具有最小和最大间隙大小的段的数量(即250 / 12.5和250/50)。由于归一化的delta在[0.1,1]的范围内,你得到的除以20和5分别给出[0.005,0.05]和[0.02,0.2]。这些范围导致第一范围的可能(标准化)步长为0.005和0.01,第二范围为0.05和0.1。

答案 1 :(得分:0)

使用deltaX

如果deltax在2到10之间增加 如果deltax在10到20单位增量之间 如果小于2,我们乘以10并再次测试 如果大于20我们分开 然后我们使用xmin得到第一个单位的位置或宽度的一半增量。

我仍然需要测试这个解决方案。

答案 2 :(得分:0)

你可能想看看Jgraph,它解决了一个互补问题:它是一个数据图示器而不是函数图示器。但是有许多共同点,例如处理主要和次要刻度线,轴标签等等。我发现输入语言对我来说有点冗长,但Jgraph产生了非常好的技术图表。网站上有很多例子,可能还有一些你可以窃取的好主意。

你知道他们说的是什么:天赋模仿,但天才偷窃: - )

答案 3 :(得分:0)

这似乎是我所期待的。

导入数学

def main():     getTickGap(-1,1.5)

def next_multiple(x,y):     return math.ceil(x / y)* y

def getTickGap(xmin,xmax):     xdelta = xmax -xmin     宽度= 250     #最小功率10大于delta     factor = 10 ** math.ceil(math.log(xdelta,10))     #0.1&lt; = normalised_delta&lt; 1     normalised_delta = xdelta / factor     print(“normalised_delta”,normalised_delta)

# we want largest gap
if normalised_delta/4 >= 0.1:
  step_size = 0.1
elif normalised_delta/4 >= 0.05:
  step_size = 0.05
elif normalised_delta/20 <= 0.01:
  step_size = 0.01
step_size = step_size * factor


##    if normalised_delta/20 == 0.005:
##      step_size = 0.005
##    elif normalised_delta/20 <= 0.01:
##      step_size = 0.01
##    elif normalised_delta/4 >= 0.05:
##      step_size = 0.05
##    step_size = step_size * factor
print("step_size", step_size)
totalsteps = xdelta/step_size
print("Total steps", totalsteps)
print("Range [", xmin, ",", xmax, "]")

firstInc = next_multiple(xmin, step_size)
count = (250/xdelta)*(firstInc - xmin)
print("firstInc ", firstInc, 'tick at ', count)
print("start at ", firstInc - xmin, (width/totalsteps)*(firstInc - xmin))
inc = firstInc

while (inc <xmax):
    inc += step_size
    count += (width/totalsteps)
    print(" inc", inc, "tick at ", count)

如果名称 ==“主要”:     main()的

答案 4 :(得分:0)

在范围-1,0

我得到了

normalised_delta 1.0
step_size 0.1
Total steps 10.0
Range [ -1 , 0 ]
firstInc  -1.0 tick at  0.0
start at  0.0 0.0
 inc -0.9 tick at  25.0
 inc -0.8 tick at  50.0
 inc -0.7 tick at  75.0
 inc -0.6 tick at  100.0
 inc -0.5 tick at  125.0
 inc -0.4 tick at  150.0
 inc -0.3 tick at  175.0
 inc -0.2 tick at  200.0
 inc -0.1 tick at  225.0
 inc -1.38777878078e-16 tick at  250.0
 inc 0.1 tick at  275.0

为什么从底部的第二行得到这个数字????

相关问题