简体   繁体   English

如何在 plot 中给定指定的 Y 值找到对应的 X 值

[英]How to find corresponding X value given a specified Y value in a plot

Currently implementing a momentum gradient descent, but I need to find the y value at the specific points x = 2.0000000052746745 for the first plot and x = 3.000000003516446 for the second plot目前正在实施动量梯度下降,但我需要在第一个 plot 的特定点 x = 2.0000000052746745 和第二个 plot 的 x = 3.000000003516446 处找到 y 值

def dz_dx(x,y):
    return (x-2)/(np.sqrt(25-(x-2)**2-(y-3)**2))

def dz_dy(x,y):
    return (y-3)/(np.sqrt(25-(x-2)**2-(y-3)**2))

xStart = 5
yStart = 5
learning_rate = 0.01
maxLimit = 10000
xStartHistory = np.zeros(maxLimit)
yStartHistory = np.zeros(maxLimit)
gamma = 0.9
update1 = 0
update2 = 0

for i in range(maxLimit):
    xStartHistory[i] = xStart
    yStartHistory[i] = yStart

    dx = dz_dx(xStart, yStart)
    dy = dz_dy(xStart, yStart)

    update1 = (gamma * update1) + (learning_rate * dx)
    update2 = (gamma * update2) + (learning_rate * dy)
    xStart = xStart - update1
    yStart = yStart - update2

print("xHistory:",xStartHistory[maxLimit-1])
print("yHistory:",yStartHistory[maxLimit-1])

figs, axs = plt.subplots(2)
axs[0].plot(xStartHistory)
axs[1].plot(yStartHistory)

You need some handler to get data from axes:您需要一些处理程序来从轴获取数据:

line0, = axs[0].plot(xStartHistory)
line1, = axs[1].plot(yStartHistory)

datax0 = line0.get_xdata()
datay0 = line0.get_ydata()

# value of y at x=2.000:
y_at_x0 = datay0[list(datax0).index(2.000)]

datax1 = line1.get_xdata()
datay1 = line1.get_ydata()
# value of y at x=3.000:
y_at_x1 = datay1[list(datax1).index(3.000)]

print(y_at_x0)
print(y_at_x1)

Further, if you need some interpolation over data you can then use numpy interpolation through griddata [ link ].此外,如果您需要对数据进行一些插值,则可以通过griddata [ 链接] 使用 numpy 插值。

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

相关问题 如何在 plot 中找到给定 y 值的对应 x 值 - How to find the corresponding x value for a given y value in a plot Python Matplotlib:在一个基本的plot上找到给定y值对应的x值 - Python Matplotlib: Find the corresponding x value of a given y value on a basic plot Python在绘图中找到x值到相应的max y值 - Python find x value to corresponding max y value in plot 查找给定z坐标值的相应[x,y]坐标 - Find the corresponding [x,y] coordinates for a given z coordinate value 如何在 Python plot 中根据给定的 X 轴值找到 Y 轴值? - How to find a Y axis value against a given X axis value in a Python plot? 在X轴上查找对应于Y的任意值的值,该值不包含在用于在python中绘制数据的列表中 - To find the value on X axis corresponding to an arbitrary value of Y that doesn't contain in the list used to plot the data in python Python Seaborn Distplot Y值对应于给定的X值 - Python Seaborn Distplot Y value corresponding to a given X value 如何从散点 plot 中找到最佳值 x 和 y 值? - How to find the optimal value x and y values from a scatter plot? 如何使用 Numpy 数组查找给定值的 x 和 y 坐标 - How to find the x and y coordinates of a given value using Numpy array 我应该如何找到对应于抛物线的最大或最小 y_value 的 x_value? (在 Python 中) - How should I find the x_value corresponding to the max or min y_value of a parabola? (in Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM