Ask Your Question

Revision history [back]

The best idea I can come up with is to write a new function which interpolates between the values in your list. Here's one easy (and probably too slow) example using linear interpolation:

def interpolate_num_func(x, num_dict):
    if x in num_dict.keys():
        return num_func[x]

    K = num_dict.keys()
    K.sort()

    # outside the range of inputs, return the first or last value
    if x < K[0]:
        return num_dict[K[0]]
    if x > K[-1]:
        return num_dict[K[-1]]

    # step through the known values until we find the two which x is between
    for i in range(1,len(K)-1):
        A = num_dict[K[i]]
        B = num_dict[K[i+1]]
        if x > K[i] and x < K[i+1]:
            t = (x-K[i])/(K[i+1]-K[i])
            return t*A + (1-t)*B # interpolate linearly

Now let's make a dict of data to plot: the keys are 'inputs' -- random numbers between 0 and 1; the values are 'outputs' -- random numbers between 0 and 10.

rand_dict = dict((random(),randrange(0,10)) for a in range(15))

And now plot the function:

var('t')
plot(lambda t: interpolate_num_func(t,rand_dict),t,0,1)