How to Change the Size of Figures in Matplotlib?
Last Updated :
15 Mar, 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:
- 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 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 heightExplanation: 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() functionExplanation: 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() functionExplanation: 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
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 Change the Figure Size with Subplots in Matplotlib
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 o
4 min read
How to Change the Font Size of Colorbars in Matplotlib
Matplotlib is a powerful and widely used library in Python for data visualization. It offers a variety of plotting functions to create complex graphs and charts. One common visualization is a heatmap, which often includes a color bar to indicate the scale of values represented by colors. Adjusting t
4 min read
How to change the size of axis labels in Matplotlib?
Matplotlib is a Python library that helps in visualizing and customizing various plots. One of the customization you can do is to change the size of the axis labels to make reading easier. In this guide, weâll look how to adjust font size of axis labels using Matplotlib.Letâs start with a basic plot
2 min read
How to change figure size in Plotly in Python
In this article, we will discuss how we can change the figure size in Plotly in Python. Let's firstly take about Plotly in Python. Plotly  Python library provides us with an interactive open-source Plotly library that can support over 40 unique chart types that cover a wide list of statistical, fin
4 min read
How can I show figures separately in Matplotlib?
In data visualization, itâs common to create multiple plots or figures for comparison, but sometimes you may need to display each plot separately. In Matplotlib, this can be easily achieved by creating and managing multiple figure objects. Most common method to create a new figure, or activate an ex
2 min read
How to Change Fonts in matplotlib?
Prerequisites: Matplotlib In this article, we will see how can we can change the font family of our graph using matplotlib. A variety of fonts are in fact supported by matplotlib, alone has to do in order to implement is pass the name as value to fontname parameter. Approach: Import required libra
2 min read
Change Font Size in Matplotlib
Matplotlib library is mainly used to create 2-dimensional graphs and plots. It has a module named Pyplot which makes things easy for plotting. To change the font size in Matplotlib, the two methods given below can be used with appropriate parameters:Â Change Font Size using fontsize You can set the
2 min read
How to Change the Number of Ticks in Matplotlib?
In this article, we will see how to change the number of ticks on the plots in matplotlib in Python. Method 1: Using xticks() and yticks() xticks() and yticks() is the function that lets us customize the x ticks and y ticks by giving the values as a list, and we can also give labels for the ticks, m
5 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 i
4 min read