Python Pandas MatPlotLib找到两条线的交点(一条曲线,一条直线)

时间:2015-12-17 21:21:43

标签: python numpy pandas matplotlib scipy

我正在尝试确定两条线的交集。

蓝线是由y计算的df['Amount']/df['SomeNumber']变量。

绿线是从2 x_coords和2 y_coords(坐标)创建的,其斜率为115.38461538461503,截距为-74.076923076922739

>>> x_coords
[0.84999999999999998, 0.97999999999999998]
>>> y_coords
[24, 39]

建议scipy.optimizefsolve或numpy的polyfit,但到目前为止我没有成功。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'SomeNumber': [0.85, 0.98, 1.06, 1.1, 1.13, 1.2, 1.22, 1.23, 1.31, 1.43],
                   'Events': [24, 39, 20, 28, 20, 24, 26, 29, 30, 24],
                   'Amount': [35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78]},
                  columns=['Amount', 'Events', 'SomeNumber'])

df = df.sort('SomeNumber')

x = df['SomeNumber']
y = df['Amount']/df['SomeNumber']

df_below = df[df['Events'] < y]
df_above = df[df['Events'] >= y]


x_coords = [df_below['SomeNumber'].min(), df_above['SomeNumber'].min()]
y_coords = [df_below.ix[df_below['SomeNumber'].idxmin(), 'Events'],
            df_above.ix[df_above['SomeNumber'].idxmin(), 'Events']]

slope, intercept = np.polyfit(x_coords, y_coords, 1)
#>>> slope, intercept == (115.38461538461503, -74.076923076922739)

plt.plot(x, y, label='Potential Events')
plt.scatter(x, df['Events'], label='Actual Events')
plt.plot(x_coords, y_coords)
plt.xlabel('Some Number')
plt.ylabel('Events')
plt.legend(loc='upper right')
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以将曲线近似为分段多项式:

p1 = interpolate.PiecewisePolynomial(x1, y1[:, np.newaxis])
p2 = interpolate.PiecewisePolynomial(x2, y2[:, np.newaxis])

p1p2x的功能。然后,您可以使用scipy.optimize.fsolve查找x等于p1(x)的{​​{1}}个值。

p2(x)

enter image description here

import pandas as pd import numpy as np from scipy import optimize from scipy import interpolate import matplotlib.pyplot as plt def find_intersections(x1, y1, x2, y2): x1 = np.asarray(x1) y1 = np.asarray(y1) x2 = np.asarray(x2) y2 = np.asarray(y2) p1 = interpolate.PiecewisePolynomial(x1, y1[:, np.newaxis]) p2 = interpolate.PiecewisePolynomial(x2, y2[:, np.newaxis]) def pdiff(x): return p1(x) - p2(x) xs = np.r_[x1, x2] xs.sort() x_min = xs.min() x_max = xs.max() x_mid = xs[:-1] + np.diff(xs) / 2 roots = set() for x_guess in x_mid: root, infodict, ier, mesg = optimize.fsolve( pdiff, x_guess, full_output=True) # ier==1 indicates a root has been found if ier == 1 and x_min < root < x_max: roots.add(root[0]) x_roots = np.array(list(roots)) y_roots = p1(x_roots) return x_roots, y_roots df = pd.DataFrame({ 'SomeNumber': [0.85, 0.98, 1.06, 1.1, 1.13, 1.2, 1.22, 1.23, 1.31, 1.43], 'Events': [24, 39, 20, 28, 20, 24, 26, 29, 30, 24], 'Amount': [35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78]}, columns=['Amount', 'Events', 'SomeNumber']) df = df.sort('SomeNumber') x = df['SomeNumber'] y = df['Amount']/df['SomeNumber'] df_below = df[df['Events'] < y] df_above = df[df['Events'] >= y] x_coords = [df_below['SomeNumber'].min(), df_above['SomeNumber'].min()] y_coords = [df_below.ix[df_below['SomeNumber'].idxmin(), 'Events'], df_above.ix[df_above['SomeNumber'].idxmin(), 'Events']] x_roots, y_roots = find_intersections(x, y, x_coords, y_coords) plt.plot(x, y, label='Potential Events') plt.scatter(x, df['Events'], label='Actual Events') plt.plot(x_coords, y_coords) plt.scatter(x_roots, y_roots, s=50, c='red') plt.xlabel('Some Number') plt.ylabel('Events') plt.legend(loc='upper right') plt.show() 附近找到了交叉点:

(0.96, 37.19)