0

How can I plot line segments in python? Here's an example of the lines I want to plot alongside scatterplot data. However, in this example, the line with slope of 1 should run from 0 to 3 only, and the line with slope of -1 should run from 4 to 8 only. I have the slope and intercept of each line, as well as the starting and ending x values.

plt.scatter(x_1, y_1)
plt.axline((0,9), slope = -1, color = 'black')
plt.axline((0,0), slope = 1, color = 'black')
plt.show()

MatplotLib Output

Mr. T
  • 11,960
  • 10
  • 32
  • 54
mishyrose
  • 1
  • 4
  • 2
    [`axline`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axline.html): "Add an infinitely long straight line." If you want a line from point A to point B, use [`plot()`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) – Mr. T Feb 22 '22 at 20:36
  • 3
    `m0=1; i0=0; x0=[0,3]; plt.plot(x0, [m0*x0[0]+i0, m0*x0[1]+i0], c='k')` – JohanC Feb 22 '22 at 20:42

3 Answers3

0

I'm not sure from your output but it looks like you have maybe two different data sets which you want to plot straight lines through. Below I am breaking 3 of your data points into two lists of x, y coordinates, and the other 4 into two lists of x, y coordinates.
Here is the link for the line of best fit one liner

import matplotlib.pyplot as plt
import numpy

# data which you want to fit lines through
x1 = [0, 1, 3]
y1 = [0, 1, 3]
x2 = [4, 5, 6, 8]
y2 = [5, 4, 3, 2]
# plots the two data set points
plt.scatter(x1, y1, color='black')
plt.scatter(x2, y2, color='blue')
# plot will connect a line between the range of the data
plt.plot(x1, y1, color='black')
plt.plot(x2, y2, color='blue')
# line of best fit one liner 
plt.plot(np.unique(x1), np.poly1d(np.polyfit(x1, y1, 1))(np.unique(x1)),color='red')
plt.plot(np.unique(x2), np.poly1d(np.polyfit(x2, y2, 1))(np.unique(x2)), color='red')
plt.ylim([-1, 7])
plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • 2
    But the OP doesn't ask how to fit the points. The start point and slope is already known. – Mr. T Feb 22 '22 at 21:28
  • But not sure how you would restrict the plotted line to a given x-range, without breaking the x-values up for your two slopes. Which I think is what you are implying within your comment to the question – transferome Feb 23 '22 at 17:28
0

By knowing both the intercept and the slope of your lines, you can essentially use the equation for a straight line to get the corresponding y-values of your endpoints on the x-axis. Then you can plot these points and style the plot to get a black line between the points. This solution looks like the following

import matplotlib.pyplot as plt
import numpy as np

slope1, intercept1 = 1, 0
slope2, intercept2 = -1, 9

l1x = np.array([0,3])
l1y = l1x*slope1+intercept1

l2x = np.array([4,8])
l2y = np.array(l2x*slope2+intercept2)

plt.plot(l1x, l1y, 'k-', l2x, l2y, 'k-')

# The following two lines are to make the plot look like the image in the question
plt.xlim([0,8])
plt.ylim([0,9])

plt.show()
eandklahn
  • 537
  • 4
  • 8
-2

The line can be defined either by two points xy1 and xy2, or by one point xy1 and a slope.

plt.scatter(x_1, y_1)
plt.axline((4,4), (8,0), color = 'blue')
plt.axline((0,0), (3,3), color = 'black')
plt.show()
Abuzar G
  • 103
  • 1
  • 4
  • 2
    This doesn't change the fact that `axline` is [*infinitely long*](https://stackoverflow.com/questions/71227725/how-can-i-plot-multiple-line-segments-in-python/71227992#comment125903476_71227725), which is *not* what OP wants, right? – BigBen Feb 22 '22 at 20:41
  • axline is not displayed as infinity long when it is bounded to two points. – Abuzar G Feb 22 '22 at 20:43
  • Emm, did you test it? https://i.stack.imgur.com/5CB4B.png. Your assumption doesn't appear to be correct. – BigBen Feb 22 '22 at 20:44
  • It's exactly what OP wants: the line with a slope of 1 should run from 0 to 3 only, and the line with a slope of -1 should run from 4 to 8 only. – Abuzar G Feb 22 '22 at 20:46
  • Look closely, your assertion is incorrect. The line extends to the very boundary of the plot, past the points you claim it stops at. – BigBen Feb 22 '22 at 20:46
  • 1
    Your words: "should". Alas, it does not. – Mr. T Feb 22 '22 at 20:47
  • For reference, here is a line that starts at 0 and ends at 3. https://i.stack.imgur.com/oI8z1.png. Your code does *not* produce this. [This code](https://stackoverflow.com/questions/71227725/how-can-i-plot-multiple-line-segments-in-python/71227992#comment125903594_71227725), using `plot`, does. – BigBen Feb 22 '22 at 20:49