A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/how-to-change-the-size-of-figures-drawn-with-matplotlib/ below:

How to Change the Size of Figures in Matplotlib?

How to Change the Size of Figures in Matplotlib?

Last Updated : 23 Jul, 2025

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:

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 Y=X^2 . 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:

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 y=x^2 . 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:

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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4