【问题标题】:Python Pandas MatPlotLib Find The Intersection Of Two Lines (One Curved, One Straight) [duplicate]Python Pandas MatPlotLib找到两条线的交点(一条曲线,一条直线)[重复]
【发布时间】:2016-03-24 12:06:24
【问题描述】:

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

蓝线是我的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()

【问题讨论】:

  • This 可能会有所帮助。

标签: python numpy pandas matplotlib scipy


【解决方案1】:

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

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

p1p2x 的函数。然后,您可以使用scipy.optimize.fsolve 查找x 值,其中p1(x) 等于p2(x)


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)附近发现了路口:

In [218]: x_roots
Out[218]: array([0.9642754164139411])

In [219]: y_roots
Out[219]: array([ 37.18562497])

【讨论】:

  • 遇到了类似的another example(就其价值而言)。我最初发现这种方法很慢,因为 X 值“大量”,所以我将蓝线范围从零限制到蓝线上方的 x、y 坐标的 x 值。我非常感谢你的帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多