Home > other >  Function with two maximum values
Function with two maximum values

Time:03-29

I have two arrays:

x = np.array([-176.72924, -171.72921, -166.72922, -161.72924, -156.72921,
       -151.72923, -146.7292 , -141.72923, -136.72925, -131.72928,
       -126.72922, -121.72924, -116.72926, -111.72929, -106.72928,
       -101.72929,  -96.72925,  -91.72923,  -86.72928,  -81.72931,
        -76.72934,  -71.72932,  -66.72936,  -61.72937,  -56.7294 ,
        -51.72934,  -46.72932,  -41.7293 ,  -36.72924,  -31.72923,
        -26.72924,  -21.72924,  -16.72925,  -11.72924,   -6.72924,
         -1.72924,    3.27076,    8.27076,   13.27076,   18.27077,
         23.27079,   28.27079,   33.27078,   38.27077,   43.27078,
         48.27076,   53.27076,   53.27079,   58.27069,   58.27074,
         63.27076,   68.27078,   73.27064,   78.27065,   83.27067,
         88.2707 ,   93.27065,   98.27067,  103.27071,  108.27064,
        113.2707 ,  118.27066,  123.27064,  128.27064,  133.27063,
        138.27062,  143.27065,  148.27064,  153.27078,  158.27077,
        163.27077,  168.27077,  173.27077,  178.27077])
y = np.array([14.80294406, 13.00826751, 11.46459466, 10.16565044,  9.08005937,
        8.176446  ,  7.44853523,  6.87122669,  6.40686981,  6.04291443,
        5.93623785,  5.58483265,  5.23970254,  4.88829734,  4.54944233,
        4.21058731,  3.90938285,  3.65210405,  3.43875089,  3.2567732 ,
        3.12499625,  3.09362078,  3.17519699,  3.38227505,  3.73368025,
       26.78209634, 26.85739746, 26.92014838, 26.88249783, 26.43696623,
       25.89730825, 23.650825  , 21.4545425 , 19.32101093, 17.27533065,
       15.8257842 , 13.67342735, 11.54617087,  9.54441625,  7.68071366,
        5.99271368,  4.49296649,  3.19402227,  2.11470629,  1.25501857,
        0.62123419,  0.20707806,  4.26706315,  3.94075832,  0.        ,
        3.73368025,  3.62072858,  3.70230479,  3.81525646,  3.99723415,
        4.23568768,  4.51806686,  4.86319697,  5.22715236,  5.59738283,
        5.97388841,  6.35039398,  6.74572483,  7.15988096,  7.62423783,
        8.15762072,  8.80395529,  9.5883419 , 28.14379149, 26.34911493,
       24.10263169, 21.63652019, 19.1704087 , 16.86744962])

Plotting these two arrays give the following scattered

Scattered plot

I need to get the coordinates of the two maximum points. Is there some function?

CodePudding user response:

I think this code might work. Let me know in case that doesn't work.

import numpy as np
    import matplotlib.pyplot as plt
    x = np.array([-176.72924, -171.72921, -166.72922, -161.72924, -156.72921,
           -151.72923, -146.7292 , -141.72923, -136.72925, -131.72928,
           -126.72922, -121.72924, -116.72926, -111.72929, -106.72928,
           -101.72929,  -96.72925,  -91.72923,  -86.72928,  -81.72931,
            -76.72934,  -71.72932,  -66.72936,  -61.72937,  -56.7294 ,
            -51.72934,  -46.72932,  -41.7293 ,  -36.72924,  -31.72923,
            -26.72924,  -21.72924,  -16.72925,  -11.72924,   -6.72924,
             -1.72924,    3.27076,    8.27076,   13.27076,   18.27077,
             23.27079,   28.27079,   33.27078,   38.27077,   43.27078,
             48.27076,   53.27076,   53.27079,   58.27069,   58.27074,
             63.27076,   68.27078,   73.27064,   78.27065,   83.27067,
             88.2707 ,   93.27065,   98.27067,  103.27071,  108.27064,
            113.2707 ,  118.27066,  123.27064,  128.27064,  133.27063,
            138.27062,  143.27065,  148.27064,  153.27078,  158.27077,
            163.27077,  168.27077,  173.27077,  178.27077])
    y = np.array([14.80294406, 13.00826751, 11.46459466, 10.16565044,  9.08005937,
            8.176446  ,  7.44853523,  6.87122669,  6.40686981,  6.04291443,
            5.93623785,  5.58483265,  5.23970254,  4.88829734,  4.54944233,
            4.21058731,  3.90938285,  3.65210405,  3.43875089,  3.2567732 ,
            3.12499625,  3.09362078,  3.17519699,  3.38227505,  3.73368025,
           26.78209634, 26.85739746, 26.92014838, 26.88249783, 26.43696623,
           25.89730825, 23.650825  , 21.4545425 , 19.32101093, 17.27533065,
           15.8257842 , 13.67342735, 11.54617087,  9.54441625,  7.68071366,
            5.99271368,  4.49296649,  3.19402227,  2.11470629,  1.25501857,
            0.62123419,  0.20707806,  4.26706315,  3.94075832,  0.        ,
            3.73368025,  3.62072858,  3.70230479,  3.81525646,  3.99723415,
            4.23568768,  4.51806686,  4.86319697,  5.22715236,  5.59738283,
            5.97388841,  6.35039398,  6.74572483,  7.15988096,  7.62423783,
            8.15762072,  8.80395529,  9.5883419 , 28.14379149, 26.34911493,
           24.10263169, 21.63652019, 19.1704087 , 16.86744962])
    
    plt.plot(x,y,'o')
    max_y = max(y)  # Find the maximum y value
    max_x = x[y.argmax()]  # Find the x value corresponding to the maximum y value
    print(max_x,max_y)
    plt.show()

CodePudding user response:

The two maximum points are the (x,y) coordinates with the highest y-value?

Start by sorting the arrays using the indices that would sort y. Then append the top 2 (or n) points to a list (if this is what you want?) and return the list:

def return_top_points(x,y,n=2):
    idx_sort = (-y).argsort()
    list_of_coordinates = [ (x[idx_sort][i], y[idx_sort][i]) for i in np.arange(n) ]
    return list_of_coordinates 

Now just call:

return_top_points(x,y,2)
  • Related