Home > Backend Development > Python Tutorial > How to Create Matplotlib Subplots with Different Widths and Heights?

How to Create Matplotlib Subplots with Different Widths and Heights?

Barbara Streisand
Release: 2024-11-30 17:19:12
Original
796 people have browsed it

How to Create Matplotlib Subplots with Different Widths and Heights?

Creating Subplots with Different Sizes Using Matplotlib

In Matplotlib, when adding multiple subplots to a figure, it may be necessary to adjust their sizes to suit specific requirements. This question aims to achieve two subplots of different widths while maintaining the same height.

Traditionally, GridSpec and its colspan argument were used for this purpose. However, in Matplotlib 3.6.0 and later, it is now possible to directly specify width and height ratios as keyword arguments to plt.subplots and subplot_mosaic:

f, (a0, a1) = plt.subplots(1, 2, width_ratios=[3, 1])
f, (a0, a1, a2) = plt.subplots(3, 1, height_ratios=[1, 1, 3])
Copy after login

Another method involves using the subplots function and passing the width ratio using gridspec_kw:

f, (a0, a1) = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
Copy after login

This approach offers greater customization options and allows for more complex subplot arrangements.

By utilizing these methods, users can easily create subplots with varying sizes within a figure, providing greater flexibility in data visualization.

The above is the detailed content of How to Create Matplotlib Subplots with Different Widths and Heights?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template