How to return a value above the lower quartile for various numbers

35 Views Asked by At

I have a Python script which will look at the maximum value for some data which is to be plotted on a y-axis. As the data is dynamic the maximum value may change. I am looking for a method to generate scales such that the quartiles are round numbers to some predefined degree. Eg. say the max value is 1015, the 1st quartile could be equal to 260, with the y-axis having an upper limit of four times this; 1040.

The code below takes 1234 as maximum value, works out a quarter and then tries to upper round this to some degree to achieve a sensible 1st quartile value.

import math

maxValue = 1234 # maximum value for the graph
a = maxValue/4.0 # lower quartile of the data
integerPart = str(a).split('.')[0] # if "a" is a decimal this selects the integer part
numberDigits = len(integerPart) # length of integerPart

def upperRound(x,N):
    return ((x+N/2) // N) * N

print "Maximum value = {}".format(maxValue)
print "Maximum value/4 = {}".format(a)
print "length of integer part = {}".format(numberDigits)
print "upperRound =", upperRound(a, 10-numberDigits)


Output:    
Maximum value = 1234
Maximum value/4 = 308.5
length of integer part = 3
upperRound = 308.0

The difficulty I am having is I only want maxValue to be user input thus the code should create appropriate values for any maxValue. In the case above, upperRound is not above the quarter value and so the graph would not fit.

I'm realise the upperRound 2nd argument is the area of concern but despite my endeavours I can't find a solution.

1

There are 1 best solutions below

0
On
maxValue = 714.5678 # maximum data value
integerPart = str(maxValue).split('.')[0] # if maxValue is a decimal this selects the integer part, ie. left of decimal
integerLength = len(integerPart)
a = 10** (integerLength-1) / 2
b = maxValue + a
c = int(round(b / a) * a)
print "Maximum value = {}".format(maxValue)
print "Axis maximum = {}".format(c)

This works nicely for integerLength => 2.