[英]Matplotlib.pyplot.ginput(): get two y-values for one x-value from two-line plot
Currently, I use the following piece of code to get function values by clicking into the plot, and this works fine for one curve:目前,我使用以下代码通过单击 plot 来获取 function 值,这适用于一条曲线:
data = pd.read_csv('filename')
plot = data.plot(x='column1', y='column2')
plt.show()
input_values = plt.ginput(n=0)
plt.close()
I would now like to simultaneously plot a second curve and obtain y-values from both plots with one click that determines the x-value.我现在想同时 plot 绘制第二条曲线,并通过单击确定 x 值从两个图中获取 y 值。 Thanks for your help!
谢谢你的帮助!
A minimal working example could look like this:一个最小的工作示例可能如下所示:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.