How to Connect Scatterplot Points With Line in Matplotlib

  1. Call Show() After Calling Both Scatter() and Plot()
  2. matplotlib.pyplot.plot() Function With the linestyle Attribute
  3. Keyword zorder to Change the Drawing Order

We can connect scatter plot points with a line by calling show() after we have called both scatter() and plot(), calling plot() with the line and point attributes, and using the keyword zorder to assign the drawing order.

Call Show() After Calling Both Scatter() and Plot()

matplotlib.pyplot.scatter(x, y) with x as a sequence of x-coordinates and y as a sequence of y-coordinates creates a scatter plot of points. To connect these points of scatter plot in order, call matplotlib.pyplot.plot(x, y) keeping x and y the same as ones passed into scatter() function.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,5,50)
y=np.sin(2 * np.pi * x)

plt.scatter(x,y)
plt.plot(x,y)
plt.title("Connected Scatterplot points with line")
plt.xlabel("x")
plt.ylabel("sinx")
plt.show()
figure.tight_layout()

Output:

Connected Scatterplot points with line_1

matplotlib.pyplot.plot() Function With the linestyle Attribute

We can also connect scatterplot points with line by just calling the matplotlib.pyplot.plot() function along with the linestyle attribute.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,5,50)
y=np.sin(2 * np.pi * x)

plt.plot(x,y,linestyle='solid',color='blue')
plt.title("Connected Scatterplot points with line")
plt.xlabel("x")
plt.ylabel("sinx")
plt.show()
figure.tight_layout()

Output:

Connected Scatterplot points with line using linestyle and color parameters

Similarly, we can try other different linestyles too

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,5,50)
y=np.sin(2 * np.pi * x)

plt.plot(x, y, 'xb-')
plt.title("Connected Scatterplot points with line")
plt.xlabel("x")
plt.ylabel("sinx")
plt.show()

Output:

Connected Scatterplot points with line using linestyle and color parameters_blue

Keyword zorder to Change the Drawing Order

We can use the keyword zorder to set the drawing order in the figure. We will assign different orders to plot and scatter and then reverse the orders to show different drawing order behaviors.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,5,50)
y=np.sin(2 * np.pi * x)

plt.scatter(x,y,color='r',zorder=1)
plt.plot(x,y,color='b',zorder=2)

plt.title("Connected Scatterplot points with line")
plt.xlabel("x")
plt.ylabel("sinx")

plt.show()

Output:

Connected Scatterplot points with line using zorder

plot() has the order as 2, larger than the order of scatter(), therefore, the scatter plot is on top of the line plot.

If we reverse the order, then the line plot will be on top of the scatter plot.

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,5,50)
y=np.sin(2 * np.pi * x)

plt.scatter(x,y,color='r',zorder=2)
plt.plot(x,y,color='b',zorder=1)

plt.title("Connected Scatterplot points with line")
plt.xlabel("x")
plt.ylabel("sinx")

plt.show()

Output:

Connected Scatterplot points with line using zorder 1

Related Article - Matplotlib Scatter

  • How to Plot List of X,y Coordinates in Matplotlib
  • How to Set Marker Size of Scatter Plot in Matplotlib
  • comments powered by Disqus