How to get X-coordinate of Path depending on the Y-coordinate when X-coordinate values are unknown using python

后端 未结 1 2018
滥情空心
滥情空心 2020-12-21 13:59

How I can get a point on the curve plotting when I have only one known Y-coordinate equation i.e. P = a * b (where a & b are defined values say 0.8,150) and x-coordinate

相关标签:
1条回答
  • 2020-12-21 14:33

    What you want is to find the root(s) or zero(s) of an array. This question's answer shows how to do that: How to get values from a graph?

    Applying the solution to this case here would look as follows:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # given values
    y = np.array([0, 38.39, 71.41, 99.66, 123.67, 143.88, 160.61, 174.03, 184.16, 190.8, 193.52])
    x = np.array([0, 0.37, 0.74, 1.11, 1.48, 1.85, 2.22, 2.59, 2.96, 3.33, 3.7])
    x_val = np.linspace(0,7) 
    
    plt.plot(x, y, '-')
    
    def find_roots(x,y):
        s = np.abs(np.diff(np.sign(y))).astype(bool)
        return x[:-1][s] + np.diff(x)[s]/(np.abs(y[1:][s]/y[:-1][s])+1)
    
    a = 0.8
    b = 150
    y_val = np.multiply(a, b)
    
    roots = find_roots(x, y-y_val)
    plt.plot(roots[0],y_val, marker="o") 
    plt.plot([roots[0],roots[0],0],[0,y_val,y_val], "--")
    
    plt.xlim(0,None)
    plt.ylim(0,None)
    plt.show()
    

    If the arrays are monotonically increasing, you may of course also simply interpolate:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # given values
    y = np.array([0, 38.39, 71.41, 99.66, 123.67, 143.88, 160.61, 174.03, 184.16, 190.8, 193.52])
    x = np.array([0, 0.37, 0.74, 1.11, 1.48, 1.85, 2.22, 2.59, 2.96, 3.33, 3.7])
    x_val = np.linspace(0,7) 
    
    plt.plot(x, y, '-')
    
    a = 0.8
    b = 150
    y_val = np.multiply(a, b)
    
    root = np.interp(y_val,y,x)
    plt.plot(root,y_val, marker="o") 
    plt.plot([root,root,0],[0,y_val,y_val], "--")
    
    plt.xlim(0,None)
    plt.ylim(0,None)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题