2

I have a linear graph, with potentially thousands or millions of data sets to plot.

Since it's a linear sequence though, I was wondering whether or not I could use given angle and inclination to fairly accurately carry on the line of regression (Or line of best fit, same thing in this context) to the edge of the graph, so I don't plot the points; which seems like a waste of processing power to me.

Here is my code so far:

    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()

This generates two lines composed of four points in total (Co-ordinates defined in tuples).

My question is, can the line be made to carry on until it reaches the edge of the graph? I tried using the line of best fit in the matplotlib.pyplot class and others too, but they don't seem to have an option to carry the line infinitely based on current data (Eg. Angle).

I'm using Python 3.5 by the way.

To clarify:

I've got a line that goes from A to B. I want to know whether or not I can see if that line (Using slope, angle, inclination or anything) can hit point D without actually drawing out the physical line. I can code that using simple trigonometric terms, but on big numbers it can be very taxing, and an infinite line of regression does not involve the same complex float point arithmetic.

Master-chip
  • 145
  • 11
  • 2
    If you can use [NumPy](http://scipy.org/), its [Polynomal Package](http://docs.scipy.org/doc/numpy/reference/routines.polynomials.package.html) is pretty awesome. You can create a *polynomial* for your fit then evaluate it at ```D```'s x coordinate. – wwii Aug 18 '16 at 14:10
  • @wwii I'm not familiar with it; but it looks pretty impressive from the documentation. How could I use it? – Master-chip Aug 19 '16 at 12:58
  • If the line is already defined, you feed the coefficients of the equation to whichever *function* will work for you. If you don't have the coefficients yet, use one of the fitting functions to get them. – wwii Aug 19 '16 at 14:37
  • @Master-chip sounds similar to this [Given n points on a 2D plane, find the maximum number of points that lie on the same straight line](https://stackoverflow.com/a/20888844/2521214) – Spektre Sep 14 '18 at 13:03

0 Answers0