How can I adjust the subplot size in Matplotlib? 2

How can I adjust the subplot size in Matplotlib?

Matplotlib is a popular Python library used for creating visualizations and plots. When working with multiple plots within a single figure, it is important to adjust the size of each subplot to ensure they are visually appealing and easy to interpret. In order to adjust the subplot size in Matplotlib, one can use the “plt.subplots” function and specify the number of rows and columns in the figure. This will create a grid of subplots, allowing for individual control of each subplot’s size and position. Additionally, the “figsize” argument can be used to set the overall size of the figure, while the “gridspec_kw” argument can be used to customize the spacing between subplots. With these tools, users can easily adjust the subplot size in Matplotlib to create professional and visually appealing visualizations.

Adjust Subplot Size in Matplotlib


You can use the following syntax to adjust the size of subplots in Matplotlib:

#specify one size for all subplots
fig, ax = plt.subplots(2, 2, figsize=(10,7))

#specify individual sizes for subplots
fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})

The following examples show how to use this syntax in practice.

Example 1: Specify One Size for All Subplots

The following code shows how to specify one size for all subplots:

import matplotlib.pyplotas plt

#define subplots
fig, ax = plt.subplots(2, 2, figsize=(10,7))
fig.tight_layout()

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#create subplots
ax[0, 0].plot(x, y, color='red')
ax[0, 1].plot(x, y, color='blue')
ax[1, 0].plot(x, y, color='green')
ax[1, 1].plot(x, y, color='purple')

We can easily change the size of the subplots by changing the values in the figsize argument:

import matplotlib.pyplotas plt

#define subplots
fig, ax = plt.subplots(2, 2, figsize=(5,5))
fig.tight_layout()

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#create subplots
ax[0, 0].plot(x, y, color='red')
ax[0, 1].plot(x, y, color='blue')
ax[1, 0].plot(x, y, color='green')
ax[1, 1].plot(x, y, color='purple')

Example 2: Specify Sizes for Individual Subplots

The following code shows how to specify different sizes for individual subplots:

import matplotlib.pyplotas plt

#define subplots
fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
fig.tight_layout()

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#create subplots
ax[0].plot(x, y, color='red')
ax[1].plot(x, y, color='blue')

We can easily change the size of the subplots by changing the values in the width_ratios argument:

import matplotlib.pyplotas plt

#define subplots
fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [1, 3]})
fig.tight_layout()

#define data
x = [1, 2, 3]
y = [7, 13, 24]

#create subplots
ax[0].plot(x, y, color='red')
ax[1].plot(x, y, color='blue')

Cite this article

stats writer (2024). How can I adjust the subplot size in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-adjust-the-subplot-size-in-matplotlib/

stats writer. "How can I adjust the subplot size in Matplotlib?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-adjust-the-subplot-size-in-matplotlib/.

stats writer. "How can I adjust the subplot size in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-adjust-the-subplot-size-in-matplotlib/.

stats writer (2024) 'How can I adjust the subplot size in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-adjust-the-subplot-size-in-matplotlib/.

[1] stats writer, "How can I adjust the subplot size in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I adjust the subplot size in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top