Home > Backend Development > Python Tutorial > How Can I Customize Subplot Sizes in Matplotlib?

How Can I Customize Subplot Sizes in Matplotlib?

DDD
Release: 2024-12-05 00:01:11
Original
1058 people have browsed it

How Can I Customize Subplot Sizes in Matplotlib?

How to Customize Subplot Sizes in Matplotlib

In Matplotlib, there are two primary methods for creating subplots: using the figure constructor or the subplots function. While the figure constructor provides more flexibility for overall figure size adjustment, it lacks the ability to control individual subplot sizes. To address this limitation, consider utilizing the subplots function with the width_ratios or height_ratios option, introduced in Matplotlib version 3.6.0.

For example, to create two subplots, one three times wider than the other, you can use the following code:

import matplotlib.pyplot as plt

# Create two subplots with a width ratio of 3:1
fig, (ax1, ax2) = plt.subplots(1, 2, width_ratios=[3, 1])
Copy after login

Alternatively, you can use the gridspec_kw argument to pass gridspec keyword arguments to the subplots function:

import matplotlib.pyplot as plt

# Create two subplots with a width ratio of 3:1 using GridSpec keyword arguments
fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
Copy after login

For vertical subplots, use height_ratios instead of width_ratios:

import matplotlib.pyplot as plt

# Create three vertically stacked subplots with height ratios of 1:1:3
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, height_ratios=[1, 1, 3])
Copy after login

By leveraging these options, you can easily customize the sizes of your Matplotlib subplots and achieve the desired figure layout.

The above is the detailed content of How Can I Customize Subplot Sizes in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template