当我只有一个已知的Y坐标方程,即P = a * b(其中a和b的定义值为0.8,150)且x坐标完全未知且没有坐标时,如何在曲线图上得到一个点连接x和y的方程(例如:y = mx + b;#我没有这种方程)。因此,现在的目标是,假设我的“ Y坐标”值为120,并且需要通过从未知的“ x坐标”值获取距离或路径来在曲线上绘制点。
我尝试了如下代码
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()
最佳答案
您要查找的是数组的根或零。该问题的答案显示了如何执行此操作:How to get values from a graph?
在这里将解决方案应用于这种情况将如下所示:
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()
如果数组是单调递增的,那么您当然也可以简单地插值:
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()
关于python - 当使用python未知X坐标值时,如何根据Y坐标获取Path的X坐标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47548611/