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
orSubFigure
, optional A unique identifier for the figure.
If a figure with that identifier already exists, this figure is madeactive 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 ornum is not given, a newfigure is created, made active and returned. Ifnum is an int, itwill be used for the
Figure.number
attribute, otherwise, anauto-generated integer value is used (starting at 1 and incrementedfor each new figure). Ifnum is a string, the figure label and thewindow title is set to this value. If num is aSubFigure
, itsparentFigure
is activated.- figsize(float, float) or (float, float, str), default:
rcParams["figure.figsize"]
(default:[6.4,4.8]
) The figure dimensions. This can be
a tuple
(width,height,unit)
, whereunit is one of "inch", "cm","px".a tuple
(x,y)
, which is interpreted as(x,y,"inch")
.
- 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 aplain
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 avoidoverlapping Axes decorations (labels, ticks, etc). Note that layoutmanagers can measurably slow down figure display.
'constrained': The constrained layout solver adjusts Axes sizesto avoid overlapping Axes decorations. Can handle complex plotlayouts and colorbars, and is thus recommended.
SeeConstrained layout guidefor examples.
'compressed': uses the same algorithm as 'constrained', butremoves extra space between fixed-aspect-ratio Axes. Best forsimple grids of Axes.
'tight': Use the tight layout mechanism. This is a relativelysimple algorithm that adjusts the subplot parameters so thatdecorations do not overlap. See
Figure.set_tight_layout
forfurther details.'none': Do not use a layout engine.
A
LayoutEngine
instance. Builtin layout classes areConstrainedLayoutEngine
andTightLayoutEngine
, more easilyaccessible by 'constrained' and 'tight'. Passing an instanceallows third parties to provide their own layout engine.
If not given, fall back to using the parameterstight_layout andconstrained_layout, including their config defaults
rcParams["figure.autolayout"]
(default:False
) andrcParams["figure.constrained_layout.use"]
(default:False
).- **kwargs
Additional keyword arguments are passed to the
Figure
constructor.
- numint or str or
- Returns:
Notes
A newly created figure is passed to the
new_manager
method or thenew_figure_manager
function provided by the currentbackend, 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 thefigure; these hooks allow arbitrary customization of the figure (e.g.,attaching callbacks) or of associated elements (e.g., modifying thetoolbar). Seemplcvd -- an example of figure hook for an example oftoolbar customization.If you are creating many figures, make sure you explicitly call
pyplot.close
on the figures you are not using, because this willenable pyplot to properly clean up the memory.rcParams
defines the default values, which can be modifiedin the matplotlibrc file.
Examples usingmatplotlib.pyplot.figure
#

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

3D voxel / volumetric plot with cylindrical coordinates

SkewT-logP diagram: using transforms and custom projections

Concatenate text objects with different properties

Complex and semantic figure composition (subplot_mosaic)