当使用python未知X坐标值时,如何根据Y坐标获取Path的X坐标 [英] How to get X-coordinate of Path depending on the Y-coordinate when X-coordinate values are unknown using python

查看:121
本文介绍了当使用python未知X坐标值时,如何根据Y坐标获取Path的X坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我只有一个已知的Y坐标方程,即P = a * b(其中a和b的定义值为0.8,150)并且x坐标完全未知时,如何在曲线图上得到一个点并且没有将x和y关联起来的方程式(例如:y = mx + b;#我没有这种方程式).因此,现在的目标是,假设我的"Y坐标"值为120,并且需要通过从未知的"x坐标"值获取距离或路径来在曲线上绘制点.

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.

我尝试了如下代码

关于我应该如何绘制的一个示例图

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

推荐答案

您想要的是查找数组的根或零.该问题的答案显示了如何执行此操作:如何从图形中获取值? /a>

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

这篇关于当使用python未知X坐标值时,如何根据Y坐标获取Path的X坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆