简体   繁体   English

Python Matplotlib:在一个基本的plot上找到给定y值对应的x值

[英]Python Matplotlib: Find the corresponding x value of a given y value on a basic plot

I have a plot generated using Matplotlib (it is a precision-recall curve of a histogram originally) and I need to compute the correct x value which corresponds to the y value with y = 0.9.我有一个使用 Matplotlib 生成的 plot(它最初是直方图的精确召回曲线),我需要计算正确的 x 值,它对应于 y = 0.9 的 y 值。 The data are loaded from text files which are present in columns.数据是从列中存在的文本文件加载的。 This is the code which is used to create the plot:这是用于创建 plot 的代码:

import numpy as np
import matplotlib.pyplot as plt
import pylab
from sklearn import metrics

data1 = np.loadtxt('text1.txt')
data2 = np.loadtxt('text2.txt')

background = 1 - (1 + y) / 2.
signal = 1 - (1 + x) / 2.

classifier_output = np.concatenate([background,signal])
true_value = np.concatenate([np.zeros_like(background, dtype=int), np.ones_like(signal,  dtype=int)])

precision, recall, threshold = metrics.precision_recall_curve(true_value, classifier_output)
plt.plot(threshold, precision[:-1])
plt.savefig('Plot.pdf', dpi = 2000)
plt.show()

Is there any way to compute the correct value on the x axis which corresponds to y = 0.9?有什么方法可以计算出 x 轴上对应于 y = 0.9 的正确值吗? 在此处输入图像描述

You can use np.interp() in the form of x_interp = np.interp(y_vals, y, x) to interpret an x value.您可以使用np.interp() x_interp = np.interp(y_vals, y, x)来解释x值。

If you want to interpret a y value instead, you need to switch to y_interp = np.interp(x_vals, x, y) .如果您想改为解释y值,则需要切换到y_interp = np.interp(x_vals, x, y)

I also added some annotations to the plot to better visualize the result.我还向 plot 添加了一些注释,以更好地可视化结果。 Since the data were not provided, I made up the data for demonstration purposes.由于没有提供数据,我自己编了个数据做演示。

import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics

# make up some data
background = np.linspace(0, 1, 400)
signal = np.linspace(0, 2, 400)

classifier_output = np.concatenate([background, signal])
true_value = np.concatenate([np.zeros_like(background, dtype=int), np.ones_like(signal, dtype=int)])

fig, ax = plt.subplots()
precision, recall, threshold = metrics.precision_recall_curve(true_value, classifier_output)
plt.plot(threshold, precision[:-1])

# added code
y_interp = 0.9
x_interp = round(np.interp(y_interp, precision[:-1], threshold), 4)  # x_interp = np.interp(y_vals, y, x)
plt.plot(x_interp, y_interp, 'o')
ax.annotate(f'(x={x_interp}, y={y_interp})', (x_interp, y_interp), size=14, xytext=(x_interp * 1.05, y_interp))
print(f'y = {y_interp}, x = {x_interp}')
plt.show()

Result:结果:

在此处输入图像描述

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

相关问题 如何在 plot 中给定指定的 Y 值找到对应的 X 值 - How to find corresponding X value given a specified Y value in a plot 如何在 plot 中找到给定 y 值的对应 x 值 - How to find the corresponding x value for a given y value in a plot Python在绘图中找到x值到相应的max y值 - Python find x value to corresponding max y value in plot 从 python plot (matplotlib) 中找到相应 x 的 y 值 - Find y value for respective x from python plot (matplotlib) 在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 查找给定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? 在matplotlib python中找到与x轴对应的值 - Find a value from x axis that correspond to y axis in matplotlib python 我应该如何找到对应于抛物线的最大或最小 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