The pyplot.scatter()
function in matplotlib
is used to create a scatter plot. For further understanding, the pyplot
module has a function called scatter()
, among many other functions, which helps to create or draw a scatter plot.
A scatter plot is a type of plot that uses a dot to graphically represent two variables for a set of data. The scatter plot is used to show a relationship between two numeric variables. The dots in a scatter plot also represent a pattern from which certain data analysis conclusions can be made.
As mentioned earlier, we make use of the pyplot.scatter()
function in matplotlib
, which creates a scatter plot.
The pyplot.scatter()
function must contain two (x and y-axis) arrays of the same length.
import matplotlib.pyplot as pltimport numpy as npx = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])y = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])plt.scatter(x, y)plt.show()
matplotlib
we imported the pyplot
module, which will help us to create plots.numpy
module, which helps us create arrays.x
and y
for both the x-axis and y-axis. They are both of equal length.pyplot.scatter()
method to create a scatter plot of x
and y
.pyplot.show()
function, we told pyplot
to show us the plot.Interestingly, we can create multiple scatter plots on the same figure. This is usually done by data analysts to compare different data variables.
import matplotlib.pyplot as pltimport numpy as np# the first scatter plotx = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])y = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])plt.scatter(x, y)# the second scatter plotx = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])y1 = np.array([5, 35, 40, 45, 80, 20, 95, 55, 70, 10])plt.scatter(x,y1)plt.show()
The two colors present in the plot are that of the first and second scatter plots. By default, pyplot
returned blue and orange colors.