How do you create line segments between two points in Matplotlib?

MatplotlibPythonData Visualization

To create line segments between two points in matplotlib, we can take the following steps

  • Set the figure size and adjust the padding between and around the subplots.
  • To make two points, create two lists.
  • Extract x and y values from point1 and point2.
  • Plot x and y values using plot() method.
  • Place text for both the points.
  • To display the figure, use show() method.


import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
point1 = [1, 2]
point2 = [3, 4]
x_values = [point1[0], point2[0]]
y_values = [point1[1], point2[1]]
plt.plot(x_values, y_values, 'bo', linestyle="--")
plt.text(point1[0]-0.015, point1[1]+0.25, "Point1")
plt.text(point2[0]-0.050, point2[1]-0.25, "Point2")


Updated on 02-Jun-2021 08:39:05