How to make two plots side-by-side using Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to make two plots side-by-side using Python in Python. So Here I am Explain to you all the possible Methods here.

How to make two plots side-by-side using Python?

1. How to make two plots side-by-side using Python?

The parameters for subplot are: number of rows, number of columns, and which subplot you're currently on. So 1, 2, 1 means “a 1-row, 2-column figure: go to the first subplot.” Then 1, 2, 2 means “a 1-row, 2-column figure: go to the second subplot.”

2. make two plots side-by-side using Python

The parameters for subplot are: number of rows, number of columns, and which subplot you're currently on. So 1, 2, 1 means “a 1-row, 2-column figure: go to the first subplot.” Then 1, 2, 2 means “a 1-row, 2-column figure: go to the second subplot.”

Method 1

plt.subplot(1, 2, 1)

...

plt.subplot(1, 2, 2)

The parameters for subplot are: number of rows, number of columns, and which subplot you’re currently on. So 1, 2, 1 means “a 1-row, 2-column figure: go to the first subplot.” Then 1, 2, 2 means “a 1-row, 2-column figure: go to the second subplot.”

You currently are asking for a 2-row, 1-column (that is, one atop the other) layout. You need to ask for a 1-row, 2-column layout instead. When you do, the result will be:

In order to minimize the overlap of subplots, you might want to kick in a:

plt.tight_layout()

before the show. Yielding:

Method 2

plt.subplots is similar. I think it’s better since it’s easier to set parameters of the figure. The first two arguments define the layout (in your case 1 row, 2 columns), and other parameters change features such as figure size:

import numpy as np
import matplotlib.pyplot as plt

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
axes.plot(x1, y1)
axes.plot(x2, y2)
fig.tight_layout()

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.