Multiple Subplots – AICorr.com



Multiple Subplots

This page covers a matplotlib multiple subplots tutorial.

Multiple subplots in Matplotlib refer to the ability to divide a single figure into a grid of smaller plots. Each of the plots can display different data or variations of the same data. The subplots are arranged in rows and columns, and as a result allowing you to create complex layouts for visualising the data.

Matplotlib uses the function subplot(). Arguments: rows, columns, index

As multiple subplots lay on the same figure, this means that each plot requires a space location. In other words, we need to choose the layout of the plots and display them accordingly. The layout of a figure is just like a table, with rows and columns.

For example, we want to show 2 plots horizontally next to each other, we need to create a figure with 1 row and 2 columns. The table below represents the idea of the example.

Or, if we want 2 plots to appear vertically in a figure, we will create it with 2 rows and 1 column.

These are the first 2 arguments in the subplot() method. There are 3 arguments in total, and the third one refers to the location (or index) of the plot.

Code implementation

Let’s look at a basic example. We will use simple bar charts. Both are identical, using the same sample data.

import matplotlib.pyplot as plt

# Sample data
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

# Bar subplot 1
plt.subplot(1, 2, 1)
plt.bar(categories, values)

# labels and title
plt.title('Bar Plot 1 Example')

# Bar subplot 2
plt.subplot(1, 2, 2)
plt.bar(categories, values)

# labels and title
plt.title('Bar Plot 2 Example')

# Show plot
plt.show()
matplotlib-horizontal-subplots-1

In the above scenario, subplot 1 has the arguments (1, 2, 1). This means that the figure has 1 rows and 2 columns and the plot is at position 1. Likewise, the second subplot has the arguments (1, 2, 2). Similarly, this indicates that the figure is with 1 rows and 2 columns, but the second plot is at position 2.

Now we can display the same plots within the same figure vertically. The only difference is that we swap the rows and columns (from 1, 2 to 2, 1).

import matplotlib.pyplot as plt

# Sample data
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

# Bar subplot 1
plt.subplot(2, 1, 1)
plt.bar(categories, values)

# labels and title
plt.title('Bar Plot 1 Example')

# Bar subplot 2
plt.subplot(2, 1, 2)
plt.bar(categories, values)

# labels and title
plt.title('Bar Plot 2 Example')

# Show plot
plt.show()
matplotlib-vertical-subplots

Benefits of Subplots

  • Comparison: You can easily compare different datasets or variations of the same dataset side by side. As a result, this makes it easier to identify patterns, trends, and differences between them.
  • Efficient use of space: Instead of creating separate figures for each plot, multiple subplots allow you to display several plots within the same figure, making more efficient use of space in your visualisations.
  • Visualisation of related data: If you have related data that you want to visualise together, multiple subplots allow you to do so while keeping each dataset visually separate.
  • Faceted plotting: Subplots enable faceted plotting, where you can partition your data based on certain variables and visualise each partition in its own subplot. This is particularly useful for exploring interactions between variables or subsets of your data.
  • Consistency: By displaying multiple plots within the same figure, you ensure consistency in terms of axes, labels, and other visual elements. Therefore making it easier for viewers to interpret the plots.
  • Contextualisation: Placing related plots next to each other provides context. Therefore it helps viewers understand the relationships between different aspects of the data more easily.
  • Presentation: When presenting your results, having multiple subplots in a single figure can help convey a comprehensive picture of your findings. This as a result can allow you to tell a more cohesive story.

For more on Matplotlib subplots, please refer to the original documentation here.


This is an original matplotlib multiple subplots tutorial. Created by aicorr.com.

Previous: Heatmaps


Discover more from reviewer4you.com

Subscribe to get the latest posts to your email.

We will be happy to hear your thoughts

Leave a reply

0
Your Cart is empty!

It looks like you haven't added any items to your cart yet.

Browse Products
Powered by Caddy

Discover more from reviewer4you.com

Subscribe now to keep reading and get access to the full archive.

Continue reading