Javatpoint Logo
Javatpoint Logo

How to change the size of figure drawn with matplotlib?

In this tutorial, we will discuss how to adjust the size of figures created with matplotlib in Python. Matplotlib library is widely used for creating graphs.

Introduction

Matplotlib is popular for creating figures for representing data. We can populate the figure with all different types of data, including axes, a graph plot, a geometric shape, etc. We can perform various operations on a matplotlib generated graph where adjusting the sizing is a common task. Sometimes, we require setting the size of the figure to a specific size. The graph can be wider in size, taller in height, etc.

The matplotlib. figure() provides the figsize attribute that specifies the width and height of a figure in-unit inches. Let's understand the following syntax.

Syntax:

The figsize is an optional parameter of figure() method. By default, the figure has the dimensions as (6.4, 4.8).

Generally, each unit inch is 80 x 80 pixels. However, we can change the pixel per unit inch using the dpi parameter, which can be specified in the same function.

We will follow the below approach.

  • We will define a variable val_1, and set it equal to plt.figure(figsize=(6,3)).
  • It will create a figure object with a width of 6 inches and 3 inches in height.
  • The figsize attribute has a value tuple of 2 values.

Let's understand the following example.

Example -

Output:

How to change the size of figure drawn with matplotlib?

Example - 2:

Output:

How to change the size of figure drawn with matplotlib?

In the above code, we have increased the height as compare to width and changed the graph plot.

We can also set the size of the subplots using the same method. Let's understand the following example.

Example -

Output:

How to change the size of figure drawn with matplotlib?

Set the Height and Width of a Figure in Matplotlib

We can also set the figure's height and width without using the figsize argument. It can be done using the set() function with the figheight and figwidth arguments. The set_figheight() and set_figwidth() functions are also used to set height and width.

Let's understand the following example.

Example -

Output:

How to change the size of figure drawn with matplotlib?

On the other hand, we can also use the set_size_inches(). Below is the example.

Example - 2:

Output:

How to change the size of figure drawn with matplotlib?

Modifying the Graph

It is another way to modify the graph attributing without using the figure environment. We can also update matplotlib.rcParams, an instance of RcParams for handling default matplotlib values.

Let's understand the following example.

Example -

Output:

How to change the size of figure drawn with matplotlib?

Conclusion

We have discussed several ways to change the size of a figure in matplotlib. These figures are very useful for data visualization. You can use any option that suit your requirement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA