Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Matplotlib中添加描述交点的线_Python_Matplotlib - Fatal编程技术网

Python 在Matplotlib中添加描述交点的线

Python 在Matplotlib中添加描述交点的线,python,matplotlib,Python,Matplotlib,我有以下代码: plt.figure(figsize=(12,8)) plt.plot(range(0, 60), cum_var,'g--', label='explained ratio') plt.plot(range(0, 60), 1 - cum_var, 'r--', label='error ratio') thre_95 = 0.95 * np.ones(60) thre_99 = 0.99 * np.ones(60) plt.plot(range(0, 60), thre_

我有以下代码:

plt.figure(figsize=(12,8))

plt.plot(range(0, 60), cum_var,'g--', label='explained ratio')
plt.plot(range(0, 60), 1 - cum_var, 'r--', label='error ratio')

thre_95 = 0.95 * np.ones(60)
thre_99 = 0.99 * np.ones(60)
plt.plot(range(0, 60), thre_95, 'k_', label='0.95 threshold')
plt.plot(range(0, 60), thre_99, 'k_', label='0.99 threshold')

idx = np.argwhere(np.isclose(thre_95, cum_var, atol=0.001)).reshape(-1)
idx[0] += 1
plt.plot(idx, thre_95[idx], 'ro')

idx = np.argwhere(np.isclose(thre_99, cum_var, atol=0.0005)).reshape(-1)
idx[0] += 1
plt.plot(idx, thre_99[idx], 'ro')

plt.legend(loc='center right')
plt.xlabel('Reduced Dimensionality')
plt.ylabel('Variance Ratio')
plt.title('Explained Ratio Curve')
plt.axis([0, 60, 0, 1])
plt.grid(True)
plt.show()
这将绘制以下图表:

现在我需要添加从交点到轴的x线和y线,以便清楚地显示什么是交点。我怎么做

编辑:我得到了答案并画出了线,现在我的问题是如何使添加线的x值显示在轴上


另一个编辑:find

那么,您需要找到两个函数(
y_1=cum_var
y_2=1-cum_var
)的交叉位置。因为其中一个是一减去另一个,所以当它们交叉时,我们知道它们的值是
0.5
。所以我们可以做一些类似的事情

# Find the index at which `cum_var` is nearest to 0.5
ind_cross = np.argmin(np.fabs(cum_var - 0.5))
# Plot vertical line there
plt.axvline(ind_cross, color='0.5', ls=':')
如果你只想让线一直延伸到他们穿过的地方,你可以这样做

plt.axvline(ind_cross, ymin=0, ymax=0.5, color='0.5', ls=':')
如果您想使其更精确,那么您可以创建一个插值,并精确地找到它穿过0.5的位置(这称为“根查找”),而不是只查找
cum_var
最接近0.5的位置,例如:

import scipy as sp
import scipy.interpolate
import scipy.optimize

# Create interpolation function from y-vales to x-vales
interp_func = sp.interpolate.interp1d(cum_var, np.arange(60))
# Find the root (so that interp_func(root) = 0.5)
root = sp.optimize.root(lamda xx: interp_func(xx) - 0.5, 4)

那么,您需要找到两个函数(
y_1=cum_var
y_2=1-cum_var
)的交叉位置。因为其中一个是一减去另一个,所以当它们交叉时,我们知道它们的值是
0.5
。所以我们可以做一些类似的事情

# Find the index at which `cum_var` is nearest to 0.5
ind_cross = np.argmin(np.fabs(cum_var - 0.5))
# Plot vertical line there
plt.axvline(ind_cross, color='0.5', ls=':')
如果你只想让线一直延伸到他们穿过的地方,你可以这样做

plt.axvline(ind_cross, ymin=0, ymax=0.5, color='0.5', ls=':')
如果您想使其更精确,那么您可以创建一个插值,并精确地找到它穿过0.5的位置(这称为“根查找”),而不是只查找
cum_var
最接近0.5的位置,例如:

import scipy as sp
import scipy.interpolate
import scipy.optimize

# Create interpolation function from y-vales to x-vales
interp_func = sp.interpolate.interp1d(cum_var, np.arange(60))
# Find the root (so that interp_func(root) = 0.5)
root = sp.optimize.root(lamda xx: interp_func(xx) - 0.5, 4)

多谢各位。这对我有帮助。我编辑后添加了一点问题,在这里找到了附加问题的答案,谢谢。这对我有帮助。我编辑添加了更多的问题,在这里找到了附加问题的答案