Automatic scale adjustment on a graph based on the maximum value (Python)

advertisements

I want to automatically set the vertical scale on a line graph, based on the maximum value in a dataset. So far I came up with this method:

if 0 < max and max <= 250:
    ymax = 250
elif 250 < max and max <= 500:
    ymax = 500
elif 500 < max and max <= 750:
    ymax = 750
elif 750 < max and max <= 1000:
    ymax = 1000

Could anyone recommend a more concise approach?


another not so intuitive implementation

def vScale(xmax):
    return ((xmax-1)/250+1)*250

and here is the result

[(x,vScale(x)) for x in xrange(0,1200,50)]

[(0, 0), (50, 250), (100, 250), (150, 250), (200, 250), (250, 250), (300, 500), (350, 500), (400, 500), (450, 500), (500, 500), (550, 750), (600, 750), (650, 750), (700, 750), (750, 750), (800, 1000), (850, 1000), (900, 1000), (950, 1000), (1000, 1000), (1050, 1250), (1100, 1250), (1150, 1250)]

And yet another implementation

limits=[0,250,500,750,1000]
ymax=limits[bisect.bisect_right(limits,xmax-1)]

Add one more to the bouquet

def vScale(xmax):
    return [250,500,750,1000][(xmax-1)/250]