Matplotlib

Scatter Plots


Scatter plots

This page covers a matplotlib scatter plots tutorial.

A scatter plot is a type of data visualisation. It displays the relationship between two numerical variables. In a scatter plot, each data point is represented by a dot. With the position of the dot on the graph corresponding to the values of the two variables. One variable is plotted on the x-axis (horizontal axis). And the other variable is on the y-axis (vertical axis).

Why use scatter plots?

Scatter plots are particularly useful for identifying patterns, trends, and correlations between variables. They can help visualise whether there is a positive or negative relationship between the variables. In addition, whether the relationship is linear or nonlinear and whether there are any outliers or clusters of data points. These plots are common in various fields such as statistics, economics, biology, and social sciences for data analysis and interpretation.

Implementation

Matplotlib provides a very easy creation of scatter plots. The main technique is such plots is scatter().

  1. Import Matplotlib library
  2. Create or import data
  3. Plot the data
  4. Add additional changes
  5. Display the data

Let’s look at an example of how to create a scatter plot.

import matplotlib.pyplot as plt

# Sample data
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]

# Scatter plot
plt.scatter(x, y)

# labels and title
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Scatter Plot Example')

# Show plot
plt.show()
matplotlib-scatter-plot

Furthermore, we can add more customisations to the chart. This is done through the plot() function. Let’s change the colour, transparency, and size of the markers. We can change the transparency of the data points through the alpha function. Please note that in a scatter plot, marker size does not use the markersize (or ms) method.

Instead, it implements it through the s (sizes) method. This technique offers more flexibility (such as different sizes for each data point).

For example:

import matplotlib.pyplot as plt

# Sample data
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]

# Scatter plot
plt.scatter(x, y, marker="+", color="green", s=274, alpha=0.2)

# labels and title
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Scatter Plot Example')

# Show plot
plt.show()
matplotlib-multiplot-chart-color-size-transparency

The methods of colour, size and transparency provide flexibility. We can adjust each data point separately. For this, we create and store values for each data point in a variable.

import matplotlib.pyplot as plt

# Sample data
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]
sizes = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650]
alphas = [0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0, 1.0]
colours = ["hotpink", "b", "r", "y", "g", "pink", "b", "r", "y", "black", "green", "white", "r"]

# Scatter plot
plt.scatter(x, y, marker="+", color=colours, s=sizes, alpha=alphas)

# labels and title
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Scatter Plot Example')

# Show plot
plt.show()
matplotlib-multiplot-chart-color-size-transparency-adjust

This is an original scatter plots tutorial. Created by aicorr.com.

Next: Bar Plots