How to connect scatterplot points with line in Matplotlib

  1. Call show() after calling both scatter() and plot()
  2. matplotlib.pyplot.plot() function with linestyle attributes
  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.png

matplotlib.pyplot.plot() function with linestyle attributes

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

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