Open In App

How to Change the Size of Figures in Matplotlib?

Last Updated : 15 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 [Tex]Y=X^2[/Tex]. The graph displays the resulting curve of this equation.

Python
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:

Screenshot-2024-12-10-155831

setting figsize with dimensions 5 inches in width and 2 inches in height

Explanation: numpy generates 100 evenly spaced x-values using np.linspace(-5, 5, 100), and y-values are computed as [Tex]y=x^2[/Tex] . The figure size is set with plt.figure(figsize=(5, 2)), creating a 5-inch wide and 2-inch high plot for a compact layout. The function is plotted using plt.plot(x, y) and titles and labels are added with plt.title(), plt.xlabel() and plt.ylabel().

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:

Python
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:

Screenshot-2024-12-10-160353

using set_figwidth() function

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:

Python
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:

Screenshot-2024-12-10-160953

using set_height() function

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

Python
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.



Similar Reads