简体   繁体   English

如何在python中获取标绘函数值

[英]How to get plotted function value in python

I have 2 discrete coordinates, X and Y. I call plt.plot(X,Y) and get nice plot. 我有2个离散坐标,X和Y。我叫plt.plot(X,Y)并得到漂亮的图。 Not I want to get the values of X(the X is that is not included in the file) from that plot(function graphic). 不是,我想从该绘图(功能图形)中获取X的值(文件中未包含X的值)。 For example I have X=[1,2,3,4] Y=[0.4,0.5, 0.8, 0.85] how to get the value for X when Y = 0.43 ? 例如我有X=[1,2,3,4] Y=[0.4,0.5, 0.8, 0.85]Y = 0.43 ?时如何获得X的值Y = 0.43 ?

With numpy: 使用numpy:

import numpy as np

X=[1,2,3,4]
Y=[0.4 ,0.5, 0.8, 0.85]

y = .43
x = np.interp(y, Y, X)

Without numpy: 没有numpy的:

X=[1,2,3,4]
Y=[0.4, 0.5, 0.8, 0.85]

y = .43

for i, yi in enumerate(Y):
    if y < yi:
        break

k = (X[i] - X[i-1])/(Y[i] - Y[i-1])
m = X[i-1]

x = k*(y-Y[i-1])+m

Note, that the function y = f(x) has to invertable (definite) and y has to be within the range of Y for this to work, as I haven't added any boundry checks. 请注意,函数y = f(x)必须可逆(确定),并且y必须在Y的范围内才能起作用,因为我没有添加任何边界检查。

What I do, is that I calculate the slope in the interesting region, and then use that to find the value at an offset from Y[i-1] 我要做的是,计算出感兴趣区域中的斜率,然后使用该斜率找到与Y[i-1]的偏移量的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM