How to Plot Multiple Graphs in Python

Visualization can help you gain a unique perspective on your data. As for tools, we have Python, a general-purpose programming language that lets you create graphs with the help of Matplotlib – open-source data visualization and graphical plotting library. Today, we are going to learn how to plot multiple graphs in Python using Matplotlib

We are using Matplotlib because it viable open-source alternative to MATLAB when it comes to drawing graphs in Python. For the tutorial, we are using Python 3.10.4 on Windows 11.

Installing Prerequisites

The first step is to make sure your system has Python and pip installed.

To check, you need to open a command prompt and run the following commands.


python --version

pip -v
Checking Python and Pip Version
Using Command Prompt to check if Python and Pip are installed

As you can see from the screenshot, we have Python 3.10.4 installed along with pip 22.0.4.

Next, we need to use the pip install command to install Matplotlib. This will install a pre-compiled package. To do so, run the following command in the command prompt.

pip install matplotlib
Checking Matplotlib Version on Windows
Checking Matplotlib version

That should install Matplotlib to your system. To check, use the following command in your Python shell.

matplotlib.__version__

 

You can also install Matplotlib from an uncompiled source, but it is not recommended as it requires a complex installation process.

Additionally, we also need a dataset to draw our graphs. For this tutorial, we are going to use the widely renowned seaborn dataset.

To install the seaborn dataset, you need to use the pip install seaborn command in your command prompt.

pip install seaborn
Installing Seaborn dataset
Installing Seaborn dataset

Plotting multiple graphs using axes

In this method, we are going to plot multiple graphs on axes. For simplicity, we are going to use the “tips” dataset.

Drawing Multiple Graphs on the same axes

Let’s first look at the code below.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')
result = df.head()

day= df["day"]
total_bill = df["total_bill"]
tip = df["tip"]

plt.plot(day, total_bill)
plt.plot(day, tip)
plt.xlabel("Day")
plt.ylabel("Total Bill and Tips")

plt.show()

Here, we imported the pyplot submodule and also the seaborn data in the first two lines.

import matplotlib.pyplot as plt
import seaborn as sns

Next, we loaded the tips dataset to our variable df

Once done, we created three variables, day, total_bill, and tip, and loaded them with their respective data values.

We now need to use the plot() function to start plotting the data against the days. The two lines that do it are:

plt.plot(day, total_bill)
plt.plot(day, tip)

Then, we labeled the x-axis and y-axis to make the chart readable.

Finally, we use plt.show() to draw the Figure showing two graphs on a single plot using axes.

But, these two graphs are combined on a SINGLE plot, which is not intended as both of these graphs use the same common axes.

To solve this, we are going to use axes() function and draw them on multiple axes.

Drawing multiple graphs on different axes()

To draw multiple graphs on different axes we need to use the axes() function.

The syntax of the axes() function is:

axes([x_lo, y_lo, width, height])

The arguments value range from 0 and 1 which corresponds to the figure dimensions.

Let’s look at the code below.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset("tips")

day= df["day"]
total_bill = df["total_bill"]
tip = df["tip"]

plt.axes([0.05, 0.05, 0.425, 0.9])
plt.plot(day, total_bill)
plt.xlabel('Day')
plt.title('Total Bill')

plt.axes([0.55, 0.05, 0.425, 0.9])
plt.plot(day, tip)
plt.xlabel('Day')
plt.title('Tip Given')

plt.show()

Here, we went forward and drew two separate graphs on the same plot, separated with the help of the axes() function.

Plotting Graphs using Axes funciton
Graph plotting using axes() function in Matplotlib

Plotting multiple graphs using Subplots() and Subplot()

Subplots()

Matplotlib offers a more convenient way to draw multiple graphs using the subplots() function. By using the subplots() function, you do not have to manually choose the Axis coordinates.

The subplot() function syntax is as below.

subplot(nrows, ncols, nsubplot) where nrows is the number of rows, ncols is the number of columns and nsubplot is the subplot number. The subplot number increases across rows and down columns, starting from 1.

Let’s take a look at the code below.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset("tips")

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,4))
sns.histplot(data=df, x='tip', ax=ax[0])
ax[0].set_title("Histogram")
sns.boxplot(data=df, x='tip', ax=ax[1]);
ax[1].set_title("Boxplot")

plt.show()

Here, we used histplot() function that lets you create a histogram. The boxplot() lets you create a boxplot to better help you understand the data.

If you run the above code, you get the following result.

subplots function to draw graphs
Using subplots() function to draw graphs without using axes() function

With subplots(), you do not have to define the axes, the space between the graphs, and other finer details.

Subplot()

In addition to the subplots() function, you can also use the subplot() function – without the “s” in the end to plot multiple graphs in Python.

The subplot() syntax is different compared to subplots() function. Let’s take a quick look at the code using the subplot() function below.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset("tips")

plt.figure(figsize =(12,5))
ax1 = plt.subplot(1,2,1)
sns.histplot(data=df, x='tip', ax=ax1)
ax1.set_title("Histogram")
ax2 = plt.subplot(1,2,2)
sns.boxplot(data=df, x='tip', ax=ax2)
ax2.set_title("Boxplot")

plt.show()

Here, we first initialized the figsize and then created separate graphs ax1 and ax2 on the same plot.

The result is as below.

Using subplot function to draw graphs
subplot() function in action

And, that’s how to draw multiple plots in Python using Matplotlib.

Was this article helpful?

Leave a Comment