How to Change the Figure Size with Subplots in Matplotlib
Last Updated :
18 Jul, 2024
Matplotlib is a powerful plotting library in Python that allows users to create a wide variety of static, animated, and interactive plots. One common requirement when creating plots is to adjust the figure size, especially when dealing with subplots. This article will guide you through the process of changing the figure size with subplots in Matplotlib, covering various methods and best practices.
Before diving into subplots, it's essential to understand how figure size works in Matplotlib. The figure size is specified in inches and can be set using the figsize
parameter. By default, Matplotlib creates figures with a width of 6.4 inches and a height of 4.8 inches. You can change these dimensions using the figure()
function or the subplots()
function.
import matplotlib.pyplot as plt
# Create a figure with a specific size
plt.figure(figsize=(8, 6)) # dimensions
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])
plt.show()
In the example above, the figure is set to 8 inches wide and 6 inches tall.
When working with subplots, you can control the overall figure size using the figsize
argument in the subplots()
function. This function allows you to create a grid of subplots within a single figure.
Python
import matplotlib.pyplot as plt
# Create a 2x2 grid of subplots with a specific figure size
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].plot([1, 2, 3, 4], [1, 4, 9, 16])
axs[0, 1].plot([1, 2, 3, 4], [1, 2, 3, 4])
axs[1, 0].plot([1, 2, 3, 4], [1, 3, 6, 10])
axs[1, 1].plot([1, 2, 3, 4], [10, 9, 8, 7])
plt.show()
Output:
Subplots with Specific Figure SizeIn this case, figsize=(10, 8)
tells Matplotlib to make the entire grid of plots 10 inches wide and 8 inches tall.
Fine-Tuning Subplot Sizes
Sometimes, you may need more control over the sizes of individual subplots. Matplotlib provides several ways to achieve this, including using the gridspec_kw
parameter and the set_size_inches()
method.
1. Using gridspec_kw
The gridspec_kw
parameter allows you to specify width and height ratios for subplots. This is useful when you want subplots to have different sizes within the same figure.
Python
import matplotlib.pyplot as plt
# Define subplots with different width ratios
fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
fig.tight_layout()
# Plot data in each subplot
ax[0].plot([1, 2, 3], [7, 13, 24], color='red')
ax[1].plot([1, 2, 3], [7, 13, 24], color='blue')
plt.show()
Output:
Using gridspec_kwIn this example, the first subplot is three times wider than the second subplot.
2. Using set_size_inches()
If you need to adjust the figure size dynamically after creating the plots, you can use the set_size_inches()
method.
Python
import matplotlib.pyplot as plt
# Create a figure and plot some data
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [1, 4, 9, 16])
# Dynamically adjust the figure size
fig.set_size_inches(4,3)
plt.show()
Output:
Using set_size_inches()Let's create a more complex figure with various subplots and customized sizes to demonstrate the concepts discussed.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
# Create a figure with a 2x2 grid of subplots
fig, axs = plt.subplots(2, 2, figsize=(12, 10))
# Plot data in each subplot
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Plot 1')
axs[0, 1].scatter(x, y)
axs[0, 1].set_title('Plot 2')
axs[1, 0].pie(x, np.abs(y))
axs[1, 0].set_title('Plot 3')
axs[1, 1].hist(y, bins=30)
axs[1, 1].set_title('Plot 4')
fig.tight_layout()
plt.show()
Output:
Creating a Complex Figure with SubplotsIn this example, we create a 2x2 grid of subplots with different types of plots (line plot, scatter plot, bar plot, and histogram). The figsize
parameter ensures that the figure is large enough to accommodate all subplots without overlapping.
Best Practices for Adjusting Subplot Sizes
When adjusting subplot sizes, it's essential to consider the following best practices to ensure your plots are clear and effective:
- Maintain Aspect Ratio: Ensure that the aspect ratio of your plots is appropriate for the data being displayed. Distorted plots can mislead the interpretation of data.
- Use Tight Layouts: Use
fig.tight_layout()
to automatically adjust subplot parameters to give specified padding. This prevents subplots from overlapping. - Consistent Sizes: Keep subplot sizes consistent when comparing similar data sets. This makes it easier to compare plots side by side.
- Readability: Ensure that labels, titles, and legends are readable. Adjust the figure size if necessary to prevent cluttering.
Conclusion
Adjusting the figure size with subplots in Matplotlib is a crucial skill for creating effective and visually appealing plots. By understanding how to use the figsize
parameter, gridspec_kw
, and set_size_inches()
, you can customize your plots to fit your specific needs. Remember to follow best practices to maintain readability and clarity in your visualizations.
Similar Reads
How to change the size of figures drawn with matplotlib?
Matplotlib provides a default figure size of 6.4 inches in width and 4.8 inches in height. While this is suitable for basic graphs, various situations may require resizing figures for better visualization, presentation or publication. This article explores multiple methods to adjust the figure size
3 min read
How to Turn Off the Axes for Subplots in Matplotlib?
In this article, we are going to discuss how to turn off the axes of subplots using matplotlib module. We can turn off the axes for subplots and plots using the below methods: Method 1: Using matplotlib.axes.Axes.axis() To turn off the axes for subplots, we will matplotlib.axes.Axes.axis() method he
2 min read
How to change the font size of the Title in a Matplotlib figure ?
In this article, we are going to discuss how to change the font size of the title in a figure using matplotlib module in Python. As we use matplotlib.pyplot.title() method to assign a title to a plot, so in order to change the font size, we are going to use the font size argument of the pyplot.title
2 min read
How to Create Different Subplot Sizes in Matplotlib?
In this article, we will learn different ways to create subplots of different sizes using Matplotlib. It provides 3 different methods using which we can create different subplots of different sizes. Methods available to create subplot: Gridspecgridspec_kwsubplot2gridCreate Different Subplot Sizes in
4 min read
How to Create Subplots in Matplotlib with Python?
Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to Change the Color of a Graph Plot in Matplotlib with Python?
Prerequisite: Matplotlib Python offers a wide range of libraries for plotting graphs and Matplotlib is one of them. Matplotlib is simple and easy to use a library that is used to create quality graphs. The pyplot library of matplotlib comprises commands and methods that makes matplotlib work like ma
2 min read
How to Add Title to Subplots in Matplotlib?
In this article, we will see how to add a title to subplots in Matplotlib? Let's discuss some concepts : Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work
3 min read
How to Add Axes to a Figure in Matplotlib with Python?
Matplotlib is a library in Python used to create figures and provide tools for customizing it. It allows plotting different types of data, geometrical figures. In this article, we will see how to add axes to a figure in matplotlib. We can add axes to a figure in matplotlib by passing a list argument
2 min read
How to Generate Subplots With Python's Matplotlib
Data visualization plays a pivotal role in the process of analyzing and interpreting data. The Matplotlib library in Python offers a robust toolkit for crafting diverse plots and charts. One standout feature is its capability to generate subplots within a single figure, providing a valuable tool for
6 min read
How to Change the Line Width of a Graph Plot in Matplotlib with Python?
Prerequisite : Matplotlib In this article we will learn how to Change the Line Width of a Graph Plot in Matplotlib with Python. For that one must be familiar with the given concepts: Matplotlib : Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a m
2 min read