How to Change the Size of Figures in 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 in Matplotlib.
Why change the figure size?
Changing the size of a figure is important for several reasons:
- Enhances readability when dealing with large datasets.
- Improves visual appeal in presentations and reports.
- Ensures proper layout in print or digital formats.
- Allows better customization for multi-panel plots.
Changing Figure Size using figsize Parameter
To change the size of a figure in matplotlib, we can use the figsize parameter of the plt.figure() function. The figsize attribute allows you to specify the width and height in inches. In the example given below we create a Matplotlib figure sized 5 inches wide by 2 inches tall to plot the quadratic equation
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
y = x**2
plt.figure(figsize=(5, 2))
plt.plot(x, y)
plt.title('Plot of y = x^2')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output:

Explanation: numpy generates 100 evenly spaced x-values using np.linspace(-5, 5, 100), and y-values are computed as
Setting Width of the Figure with set_figwidth()
set_figwidth() method allows you to adjust the width of a plot. By passing the desired width as a parameter, you can change the plot's width without affecting its default or preset height. Example:
import matplotlib.pyplot as plt
x = [1, 3, 5, 7, 9]
y = [5, 7, 2, 8, 4]
plt.figure().set_figwidth(20)
plt.plot(x, y)
plt.show()
Output:

Explanation: The figure width is set to 20 inches using plt.figure().set_figwidth(20), making the plot significantly wider for better readability. The function plt.plot(x, y) draws the line connecting the data points.
Setting Height of Figure with set_figheight()
You can use the set_figheight() method to change the height of a plot. This method will not change the default or preset value of the plot's width. Example:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [12, 5, 18, 7, 9]
plt.figure().set_figheight(5)
plt.plot(x, y)
plt.show()
Output:

Explanation: This code plots a line graph using Matplotlib while adjusting the figure height with set_figheight(5). The figure height is set to 5 inches, keeping the default width unchanged.
Changing the Size of Figure using set_size_inches()
For greater flexibility, set_size_inches() allows you to specify both dimensions explicitly. Example: Setting custom width and height
import matplotlib.pyplot as plt
fig = plt.figure()
fig.set_size_inches(5, 5)
x = [1, 2, 3, 4, 5]
y = [x*2 for x in x]
plt.plot(x, y)
plt.show()
Output:

Explanation: A figure is created with plt.figure(), set to 5×5 inches for a square aspect ratio. The x-values [1, 2, 3, 4, 5] are plotted against y-values, computed as x × 2.