Course Content
Ultimate Visualization with Python
Ultimate Visualization with Python
Scatter Plot
You did a great job learning about line plots, so now it will be much easier for you to dive into scatter plots.
A scatter plot is simply a plot which represents a relationship between two variables (x and y) using dots or other markers. Creating a scatter plot is perhaps the simplest way to check if two variables are correlated (not the most precise, but still can give some insight).
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 our case, the y values are determined linearly by the formula y = 2x + 5
. Our scatter plot visually illustrates the positive linear relationship between these two variables: y
increases with increasing x
and decreases with decreasing x
.
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 we used 'x'
as markers instead of 'o'
(dots) by default and set their size to 100. Feel free to experiment with the s
(size) parameter. We will focus more on plot customization in the next section, 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).
Swipe to show code editor
Now you will 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.
Thanks for your feedback!
Scatter Plot
You did a great job learning about line plots, so now it will be much easier for you to dive into scatter plots.
A scatter plot is simply a plot which represents a relationship between two variables (x and y) using dots or other markers. Creating a scatter plot is perhaps the simplest way to check if two variables are correlated (not the most precise, but still can give some insight).
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 our case, the y values are determined linearly by the formula y = 2x + 5
. Our scatter plot visually illustrates the positive linear relationship between these two variables: y
increases with increasing x
and decreases with decreasing x
.
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 we used 'x'
as markers instead of 'o'
(dots) by default and set their size to 100. Feel free to experiment with the s
(size) parameter. We will focus more on plot customization in the next section, 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).
Swipe to show code editor
Now you will 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.
Thanks for your feedback!
Scatter Plot
You did a great job learning about line plots, so now it will be much easier for you to dive into scatter plots.
A scatter plot is simply a plot which represents a relationship between two variables (x and y) using dots or other markers. Creating a scatter plot is perhaps the simplest way to check if two variables are correlated (not the most precise, but still can give some insight).
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 our case, the y values are determined linearly by the formula y = 2x + 5
. Our scatter plot visually illustrates the positive linear relationship between these two variables: y
increases with increasing x
and decreases with decreasing x
.
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 we used 'x'
as markers instead of 'o'
(dots) by default and set their size to 100. Feel free to experiment with the s
(size) parameter. We will focus more on plot customization in the next section, 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).
Swipe to show code editor
Now you will 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.
Thanks for your feedback!
You did a great job learning about line plots, so now it will be much easier for you to dive into scatter plots.
A scatter plot is simply a plot which represents a relationship between two variables (x and y) using dots or other markers. Creating a scatter plot is perhaps the simplest way to check if two variables are correlated (not the most precise, but still can give some insight).
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 our case, the y values are determined linearly by the formula y = 2x + 5
. Our scatter plot visually illustrates the positive linear relationship between these two variables: y
increases with increasing x
and decreases with decreasing x
.
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 we used 'x'
as markers instead of 'o'
(dots) by default and set their size to 100. Feel free to experiment with the s
(size) parameter. We will focus more on plot customization in the next section, 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).
Swipe to show code editor
Now you will 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.