Matplotlib scatter plot in Python

In this article, we will learn about the scattering of plot in matplotlib using Python. Scattering of the plot means that the point doesn’t lie on a line rather than it will be get scattered in the plot. For this, we have to use the scatter() function to plot the scattered point in the plot.

You may also read: How to plot points in matplotlib with Python

Matplotlib scatter plot in Python with examples

Let’s understand with some examples:-

  • Scattered plot of some known graph:

import matplotlib.pyplot as plt
import numpy as np

X = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
Y = np.log(X)

plt.scatter(X,Y)
plt.show()

Output:-

Matplotlib scatter plot in Python

In the above example, we executed the scattered graph of the log(x). For this, we have to first import the required module i.e, matplotlib and numpy after that we stored a numpy array from 1 to 20 in a variable named X and then established the relation between X and Y that Y = log(X). Then we used the scatter() function to plot the scattered graph of X and Y. 

  • Scattered plot of some random points:-

import matplotlib.pyplot as plt
import numpy as np

X = np.array([1,4,8,-1,0,2])
Y = np.array([3,-2,0,9,6,1])

plt.scatter(X,Y)
plt.show()

Output:-

Scattered plot of some random points in matplotlib

In this example the plotted scattered graph will look like some randomized points across the graph.

  • Scattered plot of two types of points in the same plot :-

import matplotlib.pyplot as plt
import numpy as np

X = np.array([1,4,8,-1,0,2])
Y = np.array([3,-2,0,9,6,1])
Z = np.array([0,1,4,8,2,-5])

plt.scatter(X,Y,label="Y")
plt.scatter(X,Z,cmap="g",label="Z")
plt.legend()
plt.show()

Output:-

Scattered plot of two types of points in the same plot

In this example, we plotted two different types of points in the same graph. Now, we see another example in which we will plot the two different graphs in the same frame.

  • Scattered plot of two types of points in the same plot:-

import matplotlib.pyplot as plt
import numpy as np

X = np.array([1,4,8,5,6,2])
Y = np.log(X)
Z = X**2

plt.scatter(X,Y,label="Y")
plt.scatter(X,Z,cmap="g",label="Z")
plt.legend()
plt.show()

Output :-

Scattered plot of two types of points in the same plot

Leave a Reply

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