Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib figure() Function

Matplotlib figure() Function


Matplotlib Reference Documentation Matplotlib Reference Documentation

figure() Used to create a new Figure (canvas) or activate an existing Figure.

Figure is the top-level container for all plots, managing the size, DPI, and all subplots of the entire chart.

Function definition

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *,
    facecolor=None, edgecolor=None, frameon=True,
    FigureClass=,
    clear=False, layout=None, **kwargs)

Parameter Description

Parameters Type Description
num int or str Figure identifier. An integer is a number, a string is a label. If it doesn't exist, a new one is created; if it exists, the existing one is activated
figsize tuple (w, h) Figure size (in inches), such as (8, 6). The default is determined by rcParams
dpi float Resolution (dots per inch), such as 100. Affects the pixel output size
facecolor color Figure Background Color
edgecolor color Figure frame color
frameon bool Whether to show the Figure background frame, default True
clear bool If num already exists, whether to clear it before use
layout str or LayoutEngine Recommended layout engines: 'constrained', 'compressed', 'tight', 'none'

Rarely called directly in everyday use figure(), more commonly use subplots() to create a Figure and Axes at once. Using figure() alone is mainly for multi-Figure scenarios or custom canvas properties.


Usage examples

Example 1: Creating a Figure with a custom size

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

Create a Figure with specified size and high DPI

fig

=

plt.

figure

(

figsize

=

(

10

,

4

)

,

dpi

=

100

,

                 facecolor

=

'#f8f9fa'

,

                 layout

=

'constrained'

)

Manually add subplots

ax1

=

fig.

add_subplot

(

1

,

2

,

1

)

ax2

=

fig.

add_subplot

(

1

,

2

,

2

)

x

=

np.

linspace

(

0

,

10

,

100

)

ax1.

plot

(

x

,

np.

sin

(

x

)

,

'steelblue'

,

linewidth

=

2

)

ax1.

set_title

(

'Plot 1'

)

ax2.

plot

(

x

,

np.

cos

(

x

)

,

'coral'

,

linewidth

=

2

)

ax2.

set_title

(

'Plot 2'

)

plt.

show

(

)

Example 2: Managing multiple Figures

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

linspace

(

0

,

10

,

100

)

Create Figure #1

fig1

=

plt.

figure

(

1

,

figsize

=

(

6

,

4

)

)

ax1

=

fig1.

subplots

(

)

ax1.

plot

(

x

,

np.

sin

(

x

)

,

'blue'

)

ax1.

set_title

(

'Figure 1: sin(x)'

)

Create Figure #2

fig2

=

plt.

figure

(

2

,

figsize

=

(

6

,

4

)

)

ax2

=

fig2.

subplots

(

)

ax2.

plot

(

x

,

np.

cos

(

x

)

,

'red'

)

ax2.

set_title

(

'Figure 2: cos(x)'

)

Activate Figure #1 and continue working

plt.

figure

(

1

)

plt.

plot

(

x

,

np.

sin

(

2

*x

)

,

'--'

,

alpha

=

0.5

)

Save a specific Figure

fig1.

savefig

(

'runoob_fig1.png'

,

dpi

=

150

)

fig2.

savefig

(

'runoob_fig2.png'

,

dpi

=

150

)

print

(

"runoob: two figures saved"

)

plt.

show

(

)

Example 3: Automatic layout with the layout parameter

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

Compare layout settings

Without layout (labels may overlap)

fig1

=

plt.

figure

(

1

,

figsize

=

(

8

,

4

)

)

for

i

in

range

(

3

)

:

    ax

=

fig1.

add_subplot

(

1

,

3

,

i+

1

)

    ax.

plot

(

np.

random

.

randn

(

50

)

.

cumsum

(

)

)

    ax.

set_title

(

f

'Plot {i+1}'

)

    ax.

set_xlabel

(

'A very long x label'

)

With layout='constrained' (automatically handles overlap)

fig2

=

plt.

figure

(

2

,

figsize

=

(

8

,

4

)

,

layout

=

'constrained'

)

for

i

in

range

(

3

)

:

    ax

=

fig2.

add_subplot

(

1

,

3

,

i+

1

)

    ax.

plot

(

np.

random

.

randn

(

50

)

.

cumsum

(

)

)

    ax.

set_title

(

f

'Plot {i+1}'

)

    ax.

set_xlabel

(

'A very long x label'

)

plt.

figure

(

1

)

plt.

suptitle

(

'Without layout'

,

y

=

1.02

)

plt.

figure

(

2

)

plt.

suptitle

(

'With layout="constrained"'

,

y

=

1.02

)

plt.

show

(

)

print

(

"runoob: compare layout settings"

)


Common questions

What is the unit of figsize?

Inches. Actual pixel size = figsize × dpi. For example figsize=(8,6), dpi=100 generates an 800×600 pixel image.

Which layout option is best?

'constrained' is the recommended option for most scenarios, automatically handling spacing between subplots and labels.

'compressed' compresses margins even further, suitable for scenarios that need to maximize the data area.

'none' No automatic layout (default), can be adjusted manually via subplots_adjust().

Matplotlib Reference Documentation Matplotlib Reference Documentation