目前,我使用以下代码通过单击绘图来获取函数值,这适用于一条曲线:
data = pd.read_csv('filename')
plot = data.plot(x='column1', y='column2')
plt.show()
input_values = plt.ginput(n=0)
plt.close()
我现在想同时绘制第二条曲线并通过单击确定 x 值从两个图中获取 y 值。谢谢你的帮助!
一个最小的工作示例可能如下所示:
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
#data generation
np.random.seed(123)
n=10
df = pd.DataFrame({"x": np.random.randint(1, 100, n), "y1": 2*np.arange(n), "y2": 3*np.arange(n)})
df = df.sort_values(by="x")
ax = df.plot(x='x', y=['y1', "y2"])
input_values = plt.ginput(n=1)
#retrieve x-value of the click
xpoint = input_values[0][0]
#interpolate between two data points
ypoints = [float(np.interp([xpoint], df["x"], df[col])) for col in ["y1", "y2"]]
print(xpoint, ypoints)
plt.show()
>>>53.47016129032258 [13.623870967741937, 20.435806451612905]