matplotlib.pyplot.figure#

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)[source]#

Create a new figure, or activate an existing figure.

Parameters:
numint or str or Figure or SubFigure, optional

A unique identifier for the figure.

If a figure with that identifier already exists, this figure is made active and returned. An integer refers to the Figure.number attribute, a string refers to the figure label.

If there is no figure with the identifier or num is not given, a new figure is created, made active and returned. If num is an int, it will be used for the Figure.number attribute, otherwise, an auto-generated integer value is used (starting at 1 and incremented for each new figure). If num is a string, the figure label and the window title is set to this value. If num is a SubFigure, its parent Figure is activated.

figsize(float, float), default: rcParams["figure.figsize"] (default: [6.4, 4.8])

Width, height in inches.

dpifloat, default: rcParams["figure.dpi"] (default: 100.0)

The resolution of the figure in dots-per-inch.

facecolorcolor, default: rcParams["figure.facecolor"] (default: 'white')

The background color.

edgecolorcolor, default: rcParams["figure.edgecolor"] (default: 'white')

The border color.

frameonbool, default: True

If False, suppress drawing the figure frame.

FigureClasssubclass of Figure

If set, an instance of this subclass will be created, rather than a plain Figure.

clearbool, default: False

If True and the figure already exists, then it is cleared.

layout{'constrained', 'compressed', 'tight', 'none', LayoutEngine, None}, default: None

The layout mechanism for positioning of plot elements to avoid overlapping Axes decorations (labels, ticks, etc). Note that layout managers can measurably slow down figure display.

  • 'constrained': The constrained layout solver adjusts Axes sizes to avoid overlapping Axes decorations. Can handle complex plot layouts and colorbars, and is thus recommended.

    See Constrained layout guide for examples.

  • 'compressed': uses the same algorithm as 'constrained', but removes extra space between fixed-aspect-ratio Axes. Best for simple grids of Axes.

  • 'tight': Use the tight layout mechanism. This is a relatively simple algorithm that adjusts the subplot parameters so that decorations do not overlap. See Figure.set_tight_layout for further details.

  • 'none': Do not use a layout engine.

  • A LayoutEngine instance. Builtin layout classes are ConstrainedLayoutEngine and TightLayoutEngine, more easily accessible by 'constrained' and 'tight'. Passing an instance allows third parties to provide their own layout engine.

If not given, fall back to using the parameters tight_layout and constrained_layout, including their config defaults rcParams["figure.autolayout"] (default: False) and rcParams["figure.constrained_layout.use"] (default: False).

**kwargs

Additional keyword arguments are passed to the Figure constructor.

Returns:
Figure

Notes

A newly created figure is passed to the new_manager method or the new_figure_manager function provided by the current backend, which install a canvas and a manager on the figure.

Once this is done, rcParams["figure.hooks"] (default: []) are called, one at a time, on the figure; these hooks allow arbitrary customization of the figure (e.g., attaching callbacks) or of associated elements (e.g., modifying the toolbar). See mplcvd -- an example of figure hook for an example of toolbar customization.

If you are creating many figures, make sure you explicitly call pyplot.close on the figures you are not using, because this will enable pyplot to properly clean up the memory.

rcParams defines the default values, which can be modified in the matplotlibrc file.

Examples using matplotlib.pyplot.figure#

Image

The double pendulum problem

The double pendulum problem
Image

Frame grabbing

Frame grabbing
Image

Rain simulation

Rain simulation
Image

Animated 3D random walk

Animated 3D random walk
Image

Matplotlib unchained

Matplotlib unchained
Image

Axes divider

Axes divider
Image

Demo Axes Grid

Demo Axes Grid
Image

Axes Grid2

Axes Grid2
Image

Show RGB channels using RGBAxes

Show RGB channels using RGBAxes
Image

Per-row or per-column colorbars

Per-row or per-column colorbars
Image

Axes with a fixed physical size

Axes with a fixed physical size
Image

ImageGrid cells with a fixed aspect ratio

ImageGrid cells with a fixed aspect ratio
Image

Inset locator demo

Inset locator demo
Image

Make room for ylabel using axes_grid

Make room for ylabel using axes_grid
Image

Parasite Simple2

Parasite Simple2
Image

Simple Axes Divider 1

Simple Axes Divider 1
Image

Simple axes divider 3

Simple axes divider 3
Image

Simple ImageGrid

Simple ImageGrid
Image

Simple ImageGrid 2

Simple ImageGrid 2
Image

Axis Direction

Axis Direction
Image

axis_direction demo

axis_direction demo
Image

Axis line styles

Axis line styles
Image

Curvilinear grid demo

Curvilinear grid demo
Image

Demo CurveLinear Grid2

Demo CurveLinear Grid2
Image

floating_axes features

floating_axes features
Image

floating_axis demo

floating_axis demo
Image

Parasite Axes demo

Parasite Axes demo
Image

Ticklabel alignment

Ticklabel alignment
Image

Ticklabel direction

Ticklabel direction
Image

Simple axis direction

Simple axis direction
Image

Simple axis tick label and tick directions

Simple axis tick label and tick directions
Image

Simple axis pad

Simple axis pad
Image

Custom spines with axisartist

Custom spines with axisartist
Image

Simple Axisline

Simple Axisline
Image

Simple Axisline3

Simple Axisline3
Image

Close event

Close event
Image

Interactive functions

Interactive functions
Image

Lasso Demo

Lasso Demo
Image

Barcode

Barcode
Image

Figimage Demo

Figimage Demo
Image

Image resampling

Image resampling
Image

Layer images with alpha blending

Layer images with alpha blending
Image

Errorbar limit selection

Errorbar limit selection
Image

EventCollection Demo

EventCollection Demo
Image

Filled polygon

Filled polygon
Image

Scatter plot with histograms

Scatter plot with histograms
Image

Spectrum representations

Spectrum representations
Image

Add lines directly to a figure

Add lines directly to a figure
Image

Hyperlinks

Hyperlinks
Image

Matplotlib logo

Matplotlib logo
Image

Multipage PDF

Multipage PDF
Image

Apply SVG filter to a line

Apply SVG filter to a line
Image

SVG filter pie

SVG filter pie
Image

transforms.offset_copy

transforms.offset_copy
Image

Zorder Demo

Zorder Demo
Image

Plot 2D data on 3D plot

Plot 2D data on 3D plot
Image

Demo of 3D bar charts

Demo of 3D bar charts
Image

Create 2D bar graphs in different planes

Create 2D bar graphs in different planes
Image

3D box surface plot

3D box surface plot
Image

Plot contour (level) curves in 3D

Plot contour (level) curves in 3D
Image

Plot contour (level) curves in 3D using the extend3d option

Plot contour (level) curves in 3D using the extend3d option
Image

Project contour profiles onto a graph

Project contour profiles onto a graph
Image

Filled contours

Filled contours
Image

Project filled contour onto a graph

Project filled contour onto a graph
Image

3D errorbars

3D errorbars
Image

Fill between 3D lines

Fill between 3D lines
Image

Fill under 3D line graphs

Fill under 3D line graphs
Image

Create 3D histogram of 2D data

Create 3D histogram of 2D data
Image

2D images in 3D

2D images in 3D
Image

Intersecting planes

Intersecting planes
Image

Parametric curve

Parametric curve
Image

Lorenz attractor

Lorenz attractor
Image

2D and 3D Axes in same figure

2D and 3D Axes in same figure
Image

Automatic text offsetting

Automatic text offsetting
Image

Draw flat objects in 3D plot

Draw flat objects in 3D plot
Image

Generate 3D polygons

Generate 3D polygons
Image

3D quiver plot

3D quiver plot
Image

Rotating a 3D plot

Rotating a 3D plot
Image

3D scatterplot

3D scatterplot
Image

3D plots as subplots

3D plots as subplots
Image

3D surface (solid color)

3D surface (solid color)
Image

3D surface (checkerboard)

3D surface (checkerboard)
Image

3D surface with polar coordinates

3D surface with polar coordinates
Image

Text annotations in 3D

Text annotations in 3D
Image

Triangular 3D contour plot

Triangular 3D contour plot
Image

Triangular 3D filled contour plot

Triangular 3D filled contour plot
Image

Triangular 3D surfaces

Triangular 3D surfaces
Image

More triangular 3D surfaces

More triangular 3D surfaces
Image

3D voxel / volumetric plot

3D voxel / volumetric plot
Image

3D voxel plot of the NumPy logo

3D voxel plot of the NumPy logo
Image

3D voxel / volumetric plot with RGB colors

3D voxel / volumetric plot with RGB colors
Image

3D voxel / volumetric plot with cylindrical coordinates

3D voxel / volumetric plot with cylindrical coordinates
Image

3D wireframe plot

3D wireframe plot
Image

Animate a 3D wireframe plot

Animate a 3D wireframe plot
Image

Error bar rendering on polar axis

Error bar rendering on polar axis
Image

Polar legend

Polar legend
Image

Scatter plot on polar axis

Scatter plot on polar axis
Image

Two subplots using pyplot

Two subplots using pyplot
Image

Asinh scale

Asinh scale
Image

Reference for Matplotlib artists

Reference for Matplotlib artists
Image

Drawing fancy boxes

Drawing fancy boxes
Image

Hatch demo

Hatch demo
Image

Anatomy of a figure

Anatomy of a figure
Image

Firefox

Firefox
Image

Shaded & power normalized rendering

Shaded & power normalized rendering
Image

Pan/zoom events of overlapping axes

Pan/zoom events of overlapping axes
Image

XKCD

XKCD
Image

Left ventricle bullseye

Left ventricle bullseye
Image

The Sankey class

The Sankey class
Image

Long chain of connections using Sankey

Long chain of connections using Sankey
Image

Rankine power cycle

Rankine power cycle
Image

SkewT-logP diagram: using transforms and custom projections

SkewT-logP diagram: using transforms and custom projections
Image

Cumulative distributions

Cumulative distributions
Image

Axes zoom effect

Axes zoom effect
Image

Custom Figure subclasses

Custom Figure subclasses
Image

Resize Axes with constrained layout

Resize Axes with constrained layout
Image

Resize Axes with tight layout

Resize Axes with tight layout
Image

Figure size in different units

Figure size in different units
Image

Geographic Projections

Geographic Projections
Image

GridSpec with variable sizes and spacing

GridSpec with variable sizes and spacing
Image

Gridspec for multi-column/row subplot layouts

Gridspec for multi-column/row subplot layouts
Image

Nested Gridspecs

Nested Gridspecs
Image

Manage multiple figures in pyplot

Manage multiple figures in pyplot
Image

Figure subfigures

Figure subfigures
Image

subplot2grid

subplot2grid
Image

Create multiple subplots using plt.subplots

Create multiple subplots using plt.subplots
Image

Annotate polar plots

Annotate polar plots
Image

Arrow Demo

Arrow Demo
Image

Auto-wrap text

Auto-wrap text
Image

Text rotation mode

Text rotation mode
Image

The difference between \dfrac and \frac

The difference between \dfrac and \frac
Image

Fonts demo (object-oriented style)

Fonts demo (object-oriented style)
Image

Fonts demo (keyword arguments)

Fonts demo (keyword arguments)
Image

Convert texts to images

Convert texts to images
Image

Mathematical expressions

Mathematical expressions
Image

Concatenate text objects with different properties

Concatenate text objects with different properties
Image

STIX Fonts

STIX Fonts
Image

Text properties

Text properties
Image

Unicode minus

Unicode minus
Image

Usetex text baseline

Usetex text baseline
Image

Usetex font effects

Usetex font effects
Image

Fig Axes Customize Simple

Fig Axes Customize Simple
Image

Tick formatters

Tick formatters
Image

Ellipse with units

Ellipse with units
Image

SVG Histogram

SVG Histogram
Image

Tool Manager

Tool Manager
Image

Nested GridSpecs

Nested GridSpecs
Image

Simple Legend01

Simple Legend01
Image

Menu

Menu
Image

Rectangle and ellipse selectors

Rectangle and ellipse selectors
Image

Artist tutorial

Artist tutorial
Image

Pyplot tutorial

Pyplot tutorial
Image

origin and extent in imshow

origin and extent in imshow
Image

Path effects guide

Path effects guide
Image

Transformations Tutorial

Transformations Tutorial
Image

Arranging multiple Axes in a Figure

Arranging multiple Axes in a Figure
Image

Axis ticks

Axis ticks
Image

Constrained layout guide

Constrained layout guide
Image

Complex and semantic figure composition (subplot_mosaic)

Complex and semantic figure composition (subplot_mosaic)
Image

Tight layout guide

Tight layout guide
Image

Specifying colors

Specifying colors
Image

Quick start guide

Quick start guide
Image

Annotations

Annotations
Image

Writing mathematical expressions

Writing mathematical expressions
Image

Text in Matplotlib

Text in Matplotlib
Image

Text properties and layout

Text properties and layout