如何使用Python从y值中找到x值

huangapple go评论59阅读模式
英文:

How to find x values from y value using Python

问题

我创建了一个绘图,使用了两个名为xPos(x值)和xProfile(y值)的NumPy数组。它显示在附加的图像中。

如何使用Python从y值中找到x值

这是用于绘制数值的我的代码:

xProfile = 100*(eDepMat[:,41,41]/eDepMat[41,41,41])
xPos = np.linspace(-20, 20, 81)
plot4 = ax4.plot(xPos, xProfile)
ax4.set_xlabel('x positions [cm]')
ax4.set_ylabel('Relative dose [%]')
ax4.grid(color='black', linestyle='-', linewidth=0.1)
ax4.set_title('X Profile')

我想要使用Python从y值=50找到x值。
我尝试使用np.interp(),但是结果是不正确的。结果应该接近+10.45或-10.45。

np.interp(50, xPos, xProfile)
Out[60]: 6.488597663333305

np.interp(50, xProfile, xPos)
Out[61]: 20.0

可以有人帮我吗?非常感谢。

我需要一小段代码来解决这个问题。

英文:

I created a plotting from 2 numpy arrays named xPos (x Value) and xProfile (y Value). It is show in the attached image.

如何使用Python从y值中找到x值

Here is my code to plot values:

xProfile = 100*(eDepMat[:,41,41]/eDepMat[41,41,41])
xPos = np.linspace(-20, 20, 81)
plot4 = ax4.plot(xPos, xProfile)
ax4.set_xlabel('x positions [cm]')
ax4.set_ylabel('Relative dose [%]')
ax4.grid(color='black', linestyle='-', linewidth=0.1)
ax4.set_title('X Profile')

# xProfile=[1.65357
1.44279
1.99039
2.11737
2.41571
2.20419
2.64493
2.90684
3.90013
4.3398
5.64764
7.28404
9.31809
10.6319
12.9572
19.3046
24.318
28.3353
35.8915
43.3915
53.1689
59.4102
69.7083
81.97
85.2627
89.1488
95.4716
96.9098
98.7664
98.7688
98.1199
97.5785
99.2019
97.9184
101.332
99.6836
98.8609
99.778
101.83
98.1091
101.7
100
102.626
102.36
99.5246
104.03
104.136
106.218
106.162
103.319
105.376
104.213
103.848
102.196
103.402
99.6002
91.5528
85.2763
77.0413
63.0445
53.9208
44.4396
34.8563
29.3416
22.0063
17.2319
12.4915
10.3133
7.72007
5.79157
4.3541
3.97394
2.9509
2.72589
2.71177
2.61017
2.00052
1.96052
1.66793
1.36369
1.91262
]

I would like to find x Value from y Value = 50 using Python.
I tried to use np.interp(), but is was incorrect. The result must be ~+10.45 or ~-10.45

np.interp(50, xPos, xProfile)
Out[60]: 6.488597663333305

np.interp(50, xProfile, xPos)
Out[61]: 20.0

Can anyone help me please. Thank you in advance.

I need a bit of code for the solution.

答案1

得分: 2

这是一种简单/天真的方法来做到这一点:

y_val = 50 # < -- 在这里更改 y 值

l_idx = np.abs(xProfile - y_val).argmin()
r_idx = np.abs(np.flip(xProfile) - y_val).argmin()

x1_val = xPos[l_idx] # -10.0
x2_val = xPos[r_idx] # +10.0

ax4.plot(x1_val, y_val, 'ro', label='y=50')
ax4.plot(x2_val, y_val, 'ro', label='y=50')

ax4.axhline(y=y_val, color='gray', linestyle='--')

ax4.axvline(x=x1_val, ymin=0, ymax=y_val/100, color='gray', linestyle='--')
ax4.axvline(x=x2_val, ymin=0, ymax=y_val/100, color='gray', linestyle='--')

plt.show();

如何使用Python从y值中找到x值

英文:

Here is a simple/naïve way to do it :

y_val = 50 # &lt;-- change here the y-value

l_idx = np.abs(xProfile - y_val).argmin()
r_idx = np.abs(np.flip(xProfile) - y_val).argmin()

x1_val = xPos[l_idx] # -10.0
x2_val = xPos[r_idx] # +10.0

ax4.plot(x1_val, y_val, &#39;ro&#39;, label=&#39;y=50&#39;)
ax4.plot(x2_val, y_val, &#39;ro&#39;, label=&#39;y=50&#39;)

ax4.axhline(y=y_val, color=&#39;gray&#39;, linestyle=&#39;--&#39;)

ax4.axvline(x=x1_val, ymin=0, ymax=y_val/100, color=&#39;gray&#39;, linestyle=&#39;--&#39;)
ax4.axvline(x=x2_val, ymin=0, ymax=y_val/100, color=&#39;gray&#39;, linestyle=&#39;--&#39;)

plt.show();

如何使用Python从y值中找到x值

答案2

得分: 1

此解决方案假设您可以在线性插值两个点之间的数据。

y_target = 50
b = np.sign(xProfile - y_target)
b = (b[:-1]*b[1:] < 0)  # 在两个元素之间的y_target为负值
b = np.where(b)
x_vals = []
for idx in b:
    # 使用线性插值
    fac = (y_target-xProfile[idx])/(xProfile[idx+1]-xProfile[idx])
    x_vals.append( (1-fac)*xPos[idx]+fac*xPos[idx+1] )
print(x_vals)

# [array([-10.16205228,  10.20676708])]
英文:

This solution assumes that you can linearly interpolate the data between two points.

y_target = 50
b = np.sign(xProfile - y_target)
b = (b[:-1]*b[1:] &lt; 0) # negative where y_target between two elements
b = np.where(b)
x_vals = []
for idx in b:
  # use linear interpolation
  fac = (y_target-xProfile[idx])/(xProfile[idx+1]-xProfile[idx])
  x_vals.append( (1-fac)*xPos[idx]+fac*xPos[idx+1] )
print(x_vals)

# [array([-10.16205228,  10.20676708])]

huangapple
  • 本文由 发表于 2023年5月25日 11:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定