给定倾角和角度的无限线性回归线

时间:2016-08-17 13:10:35

标签: python python-3.x math matplotlib 2d

我有一个线性图,可能需要数千或数百万个数据集。

因为它是一个线性序列,我想知道我是否可以使用给定的角度和倾向来相当准确地进行回归线(或最佳拟合线,在这种情况下同样的事情)到图的边缘,所以我不绘制点;这似乎是对我的处理能力的浪费。

到目前为止,这是我的代码:

    def get_angle_plot(line1, line2, offset = 1, color = None, origin = [0,-1], len_x_axis = 1, len_y_axis = 1):

        # Angle between line1 and x-axis
        slope1, intercept, r_value, p_value, std_err = stats.linregress([0,14321], [-1,0]) 
        angle1 = abs(math.degrees(math.atan(slope1))) # Taking only the positive angle

        # Angle between line2 and x-axis
        slope2, intercept, r_value, p_value, std_err = stats.linregress([0,1], [-1,-1])   
        angle2 = abs(math.degrees(math.atan(slope2)))

        theta1 = min(angle1, angle2)
        theta2 = max(angle1, angle2)

        angle = theta2 - theta1

        if color is None:
            color = line1.get_color() # Uses the color of line 1 if color parameter is not passed.

        return Arc(origin, len_x_axis*offset, len_y_axis*offset, 0, theta1, theta2, color=color, label = str(angle)+u"\u00b0")

    def get_angle_text(angle_plot):
        angle = angle_plot.get_label()[:-1] # Excluding the degree symbol
        angle = "%0.2f"%float(angle)+u"\u00b0" # Display angle upto 2 decimal places

        # Get the vertices of the angle arc
        vertices = angle_plot.get_verts()

        # Get the midpoint of the arc extremes
        x_width = (vertices[0][0] + vertices[-1][0]) / 2.0
        y_width = (vertices[0][1] + vertices[-1][0]) / 2.0

        separation_radius = max(x_width/2.0, y_width/2.0)

        return [ x_width + separation_radius, y_width + separation_radius, angle]   


    fig = plt.figure()

    line_1 = Line2D([0,14321], [-1,0], linewidth=1, linestyle = "-", color="green")
    line_2 = Line2D([0,1], [-1,-1], linewidth=1, linestyle = "-", color="red")

    ax = fig.add_subplot(1,1,1)

    ax.add_line(line_1)
    ax.add_line(line_2)

    angle_plot = get_angle_plot(line_1, line_2, 1)
    angle_text = get_angle_text(angle_plot) 
    # Gets the arguments to be passed to ax.text as a list to display the angle value besides the arc

    ax.add_patch(angle_plot) # To display the angle arc
    ax.text(*angle_text) # To display the angle value


    plt.legend()
    plt.show()

这会生成由总共四个点组成的两条线(在元组中定义的坐标)。

我的问题是,这条线是否可以继续直到它到达图的边缘?我尝试在matplotlib.pyplot类和其他类中使用最佳拟合线,但它们似乎没有根据当前数据(例如角度)无限地携带线的选项。

我顺便使用Python 3.5。

澄清:

我有一条从A到B的线。我想知道我是否可以看到该线(使用斜率,角度,倾角或任何东西)是否可以达到D点而不实际抽出物理线。我可以使用简单的三角项来编码,但在大数字上它可能非常繁琐,并且无限回归线不涉及相同的复杂浮点算法。

0 个答案:

没有答案