Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Scatter Plot | Creating Commonly Used Plots
Ultimate Visualization with Python

Swipe to show menu

book
Scatter Plot

Note
Definition

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:

1234567
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()
copy

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:

1234567
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()
copy

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.

Note
Study More

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).

Note
Note

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.
Task

Swipe to start coding

Display a quadratic relationship between two variables using a scatter plot:

  1. Replace the underscores, so that y array contains squared elements of the x array.
  2. Use the correct function to create a scatter plot.
  3. Pass x and y in this function in the correct order.
  4. Set the size of the markers to 70.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
Scatter Plot

Note
Definition

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:

1234567
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()
copy

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:

1234567
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()
copy

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.

Note
Study More

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).

Note
Note

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.
Task

Swipe to start coding

Display a quadratic relationship between two variables using a scatter plot:

  1. Replace the underscores, so that y array contains squared elements of the x array.
  2. Use the correct function to create a scatter plot.
  3. Pass x and y in this function in the correct order.
  4. Set the size of the markers to 70.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt