Data Visualization Tutorial

Hands-on Scatter Plot with Matplotlib in Python

A practical guide on how to draw excellent scatter plots with the plot and scatter methods.

Tirendaz AI
Level Up Coding
Published in
7 min readMar 7, 2021

--

Photo by Count Chris on Unsplash

A scatter plot is a type of plot used to display the values of two variables. Scatter plot is also called scatter chart, scatter graph, or scatter diagram. In this post, I’ll explain the following topics:

  • What is scatter plot?
  • Scatter plot with the plot method
  • Scatter plot with the scatter method
  • The scatter method vs the plot method

Let’s dive in!

What is scatter plot?

A scatter plot is a type of data display that uses dots to represent values for two different numeric variables. You can draw a scatter plot to see the relationship between two numerical variables such as weight and height. You can find the correlations between the variables with the scatter plot.

Let’s draw a scatter plot using the matplotlib. To show this, let’s import the necessary libraries.

You can find the notebook and dataset here. Now, let’s use the %matplotlib inline magic command to enable the inline plotting.

Let’s specify the style of the graphics with seaborn.

I’m going to use the iris dataset to show the scatter plot. Let’s load the iris dataset with seaborn.

Let’s see the first rows of the dataset.

There are 4 numeric variables in the dataset, they are sepal height and width, petal height and width, and one variable indicating three types of the iris flower. You can draw a scatter plot with the plot method and the scatter method. First, I’m going to show how to use the plot method.

Scatter plot with the plot method

To show how to draw a scatter plot with the plot method, let’s create a graphic object and graphic area.

Let’s draw a scatter plot of sepal length and sepal width variables.

Scatter plot of sepal length and sepal width variables

You can change the shape of the dots. To do this, let’s use the + symbol.

Scatter plot with +

Let’s see the scatter plot with triangulars using the v option.

Scatter plot with triangular

The color of the dots is blue by default. You can change the color. For example, let’s use the color red with the rv option.

Scatter plot with triangular and red

You can also specify the size of the dots with the marksize parameter as follows:

Scatter plot with triangular and red

Scatter plot with the scatter method

You can also draw the scatter plot with the scatter method. This method is similar to the plot method. Let’s draw a scatter plot of the sepal width and petal width with this method.

Scatter plot with scatter method

You can adjust the size of the dots with the s parameter.

Scatter plot with the s parameter

The scatter method vs the plot method

The plot method is faster than the scatter method for large samples. In addition, the properties of the dots are entered separately in the scatter method. The c parameter is used for the color of the dots. For example, let’s use the color purple.

Scatter plot with the color purple

You can use the marker parameter for the shape of the dots. For example, let’s draw the scatter plot with the triangles.

Scatter plot with the triangles

You can use the edgecolor parameter for the border color of the dots, and linewidth for the thickness.

Scatter plot with border colors of the dots

You can also adjust the visibility of the dots with the alpha parameter.

Scatter plot using the alpha parameter

You can assign each dot to a color. To show this, let’s create a color variable for this and generate 150 numbers between 0 and 10 with the random.randint method.

Next, let’s set this color variable to the c parameter.

Scatter plot with the colors

You can adjust the color of the dots with the cmap method. When setting the color of the cmap method, you need to start the first letter with a capital letter and end with the letter s.

Scatter plot

For more different colors, you can use the "viridis" option.

Scatter plot with viridis option

You can also add a bar that displays the numerical values of the colors with the colorbar method.

Scatter plot with colorbar

You can also name the color bar with the set_label method.

Scatter plot with color bar

In addition, you can adjust the size of the dots. For this, let’s create a variable with the random.randint method.

Next, let’s set this variable to the parameter s.

Scatter plot with different sizes of dots

You can name the axes with the xlabel and the ylabel methods.

Scatter plot with axis names

Finally, let’s give the plot a title with the title method.

Scatter plot with a title

Conclusion

You can use a scatter plot to see the relationship between two variables. In this post, I talked about the scatter plot using plot and scatter methods. That’s it. I hope you enjoy it. Thank you for reading. You can find this notebook here. Don’t forget to follow us on YouTube | GitHub | Twitter | Kaggle | LinkedIn

Data Visualization with Python

11 stories

If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇

--

--