当使用python未知X坐标值时,如何根据Y坐标获取Path的X坐标

编程入门 行业动态 更新时间:2024-10-27 13:21:30
本文介绍了当使用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坐标

本文发布于:2023-07-19 09:01:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:坐标   坐标值   python   Path

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!