Home > Backend Development > Python Tutorial > How Can I Create Matplotlib Subplots with Varied Sizes?

How Can I Create Matplotlib Subplots with Varied Sizes?

DDD
Release: 2024-11-26 14:25:10
Original
620 people have browsed it

How Can I Create Matplotlib Subplots with Varied Sizes?

Figure Configuration with Varied Subplot Sizes

When creating subplots within a figure, it's often desirable to customize their sizes. Matplotlib provides two approaches to achieve this:

Using GridSpec with the colspan Argument

Initially, the user attempted to use GridSpec's colspan argument to control subplot widths. However, this approach is only suitable for adjusting the first subplot's size.

Direct Configuration with width_ratios and height_ratios

As of Matplotlib version 3.6.0, width_ratios and height_ratios can be passed directly to plt.subplots and subplot_mosaic. Using this technique:

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

creates a figure with a subplot three times wider than the second. Similarly, the following code adjusts the height ratio of three subplots:

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

Employing subplots and gridspec_kw

Another option is to use subplots with gridspec_kw, specifying the width ratio through the width_ratios parameter. This approach allows for more customization options.

For instance, the following code generates subplots with a width ratio of 3:1:

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

Vertical subplots can be configured similarly by adjusting height_ratios in gridspec_kw.

The above is the detailed content of How Can I Create Matplotlib Subplots with Varied Sizes?. 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