Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

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

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 is totally unknown and there is no equation linking x and y ( ex: y = mx +b; # i don't have this kind of equations). So, now the target is if say I have 'Y-coordinate' value as 120 and need to plot a point on the curve by taking distance or path from the unknown 'x-coordiante' value.

I tried the code as below

One sample figure about how I should plot

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline

# 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) #limts on x-axis
a = 0.8
b = 150
y_val = np.multiply(a, b)
yinterp = np.interp(x_val, x, y)
plt.plot(x, y, '-')
plt.plot(x_val, yinterp, 'o')

#here i need to plot a exact point w.r.t to y_val
#and also need to show the distance with a line from the selected x and y coordinates

plt.plot(x_val,y_val, '--') 
plt.show()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

enter image description here

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...