Sum over parametric function
Question: I am dealing with a parametric function like this:

[![Parametric
function](http://i.stack.imgur.com/RA0Dy.png)](http://i.stack.imgur.com/RA0Dy.png)

and Ideally I would like to sum over the repeated x axis, as shown in the
example. It is to say, for x~4.75, I see that the function can be 0.04, 0.06
or 0.16, and I would like to add a point to the sum of 0.06+0.04+0.16 = 0.26.
I need to do that for every point, so that I can construct a function which is
a kind of 'projection' of the parametric one. Anyone has an idea on how can I
do that in Python?


Answer: Look at the example:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    # set x, y
    x = np.arange(-3.,3.,.1)
    N = x.size
    x[10:13] = x[10]
    y = x ** 3 + np.random.rand(N)
    
    # plot curve
    fig, ax = plt.subplots()
    plt.plot(x,y,'b-')
    curve = ax.lines[0]
    
    # get data of plotted curve
    xvalues = curve.get_xdata()
    yvalues = curve.get_ydata()
    
    # get y for given x 
    indexes = np. where(xvalues == x[10]) 
    # test print
    print xvalues[indexes] 
    print yvalues[indexes]
    print "Sum of y(x) = ",np.sum(yvalues[indexes]) , " where x = ", x[10]
    
    # define markers
    xm = []
    ym = []
    
    for x1 in x:
        indexes = np.where(xvalues == x1) 
        print x1, yvalues[indexes]
        if len(yvalues[indexes]) > 1:
            xm += [xvalues[indexes],]
            ym += [np.sum(yvalues[indexes]),]
    
    plt.plot(xm, ym, linestyle = 'None', marker='o', color='g')
    
    plt.show()
    

Test output:

    
    
    x: [-2. -2. -2.]
    y: [-7.0936372  -7.42647923 -7.56571131]
    Sum of y(x) =  -22.0858277351  where x =  -2.0
    

[![enter image description
here](http://i.stack.imgur.com/9CKRR.png)](http://i.stack.imgur.com/9CKRR.png)

