Connect points with line in Matplotlib

In this article, we are going to learn how we can connect points on a plot with a line in Matplotlib Python library. At times when working with a scatter plot, we need to connect all the points with a line. This is to see the variations in the dependent variable with the increasing or changing the independent variable. Let see how we can do this with some easy code.

Scatter plot

Before moving towards connecting some points, let us plot some random scatter plot. We would use this plot as a reference on which we would plot the line.

Consider the Python code below for the scatter plot:

# Importing the libraries
import matplotlib.pyplot as plt
import random
import numpy as np
np.random.seed(10)
# Random X and Y points for the scatter plot
x = np.arange(0,30,1)    
y = [np.random.randint(1,50) for i in x]

fig = plt.figure(figsize=(10,8))  #setting the figure size
plt.scatter(x, y,marker='o',color='r')# plotting the scatter plot
plt.title('Scatter plot') # Title of the figure

# Labelling the x and y axis
plt.xlabel('X-axis') 
plt.ylabel('Y-axis')
plt.show()

matplotlib red points

Connecting points

All the points can be connected by a line in an easy way. Just plot the line plot for the corresponding data values for the x and y-axis that were used for plotting the above scatter plot.

See the code below which adds a line plot on the above scatter plot.

# Importing the libraries
import matplotlib.pyplot as plt
import random
import numpy as np
np.random.seed(10)
# Random X and Y points for the scatter plot
x = np.arange(0,30,1)    
y = [np.random.randint(1,50) for i in x]

fig = plt.figure(figsize=(10,8))  #setting the figure size
plt.scatter(x, y,marker='o',color='r',zorder=1)# plotting the scatter plot

# adding the line plot for the above points.
plt.plot(x,y,'.b-',zorder=2) # a blue color line

plt.title('Scatter plot') # Title of the figure

# Labelling the x and y axis
plt.xlabel('X-axis') 
plt.ylabel('Y-axis')
plt.show()

connect points matplotlib

Note:  In the above plot, the line is a plot over the scatter points. To plot the scatter points over the plotted line, you can change the zorder parameter to change the order of two plots. In the above code change, the order of the plots by changing the values form 1 to 2 and vice versa.

See the code below and output for the above process.

# Importing the libraries
import matplotlib.pyplot as plt
import random
import numpy as np
np.random.seed(10)
# Random X and Y points for the scatter plot
x = np.arange(0,30,1)    
y = [np.random.randint(1,50) for i in x]

fig = plt.figure(figsize=(10,8))  #setting the figure size
plt.scatter(x, y,marker='o',color='r',zorder=2)# plotting the scatter plot

# adding the line plot for the above points.
plt.plot(x,y,'.b-',zorder=1) # a blue color line

plt.title('Scatter plot') # Title of the figure

# Labelling the x and y axis
plt.xlabel('X-axis') 
plt.ylabel('Y-axis')
plt.show()

connect point 3 matplotlib

You can clearly see the difference in the above plot.

I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.

You can also read other articles related to this. Click the links given below.

Leave a Reply

Your email address will not be published. Required fields are marked *