Scatter Plot
A scatter plot is a type of plot that displays the relationship between two variables (x and y) using dots or other markers.
Scatter plots are one of the simplest tools to visually examine whether two variables are correlated. While not the most precise method, they often provide useful insight at a glance.
It is similar to a line plot, except for the fact that it has no lines, only markers. In order to create a scatter plot, all you have to do is to use a scatter()
function of the pyplot
, passing first values for x-axis, then values for y-axis. Let's have a look at an example:
import matplotlib.pyplot as plt import numpy as np x = np.array([6, 1, 8, 20, 13, 4, 16, 5, 11, 10]) y = 2 * x + 5 # Creating a scatter plot plt.scatter(x, y) plt.show()
The scatter()
function's syntax resembles that of plot()
. However, unlike plot()
, you must always provide values for both x
and y
parameters.
In this example, y
is calculated using the formula y = 2x + 5
. The scatter plot shows a positive linear relationship β as x
gets bigger, y
also increases, and in reverse way too.
It is also possible to set other markers instead of dots and set their size using marker
and s
parameters respectively:
import matplotlib.pyplot as plt import numpy as np x = np.array([6, 1, 8, 20, 13, 4, 16, 5, 11, 10]) y = 2 * x + 5 # Specifying the markers and their size plt.scatter(x, y, marker='x', s=100) plt.show()
Here, 'x'
markers were used instead of the default 'o'
(dots), and their size was set to 100. You can adjust the s
parameter to explore different marker sizes.
The next section will focus more on plot customization, but, as for now, you can use scatter()
function documentation to explore more.
Plotting multiple scatter plots can be accomplished simply by calling the scatter()
function twice with different x
and y
arguments (similarly to line plots).
While plt.plot(x, y, 'o')
and plt.scatter(x, y)
may look similar at first glance, they serve different purposes:
plt.plot(x, y, 'o')
is a line plot function using'o'
to show only markers. It's quick but offers limited styling.plt.scatter(x, y)
is a dedicated scatter plot function that provides more control β allowing individual customization of marker size, color, and transparency.
Swipe to start coding
Display a quadratic relationship between two variables using a scatter plot:
- Replace the underscores, so that
y
array contains squared elements of thex
array. - Use the correct function to create a scatter plot.
- Pass
x
andy
in this function in the correct order. - Set the size of the markers to 70.
Solution
Thanks for your feedback!