How to modify existing figure instance in Matplotlib?
Last Updated :
13 Dec, 2021
In Python matplotlib.pyplot.figure() is used to modify existing Figure instance or make a new Figure instance. Generally, it is used to alter the basic properties of existing plots. It takes references to the concerned plots in case if it is used to alter the properties of the already formed plots. It returns the Figure instance and passes the same to new_figure_manager in the backend, which in return allows to custom hook Figure classes into the pyplot interface.
Syntax: matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None,
frameon=True, FigureClass=<class ‘matplotlib.figure.Figure’>, clear=False, **kwargs)
Parameters
num : It is a unique identifier for the figure. If the Figure instance is already there with the same value, that instance is returned else a new instance is created and random number assigned to it. In case the value is number then it acts as a unique id and in case of string it also acts as Image label. This parameter is optional.
figsize: It is a parameter which takes in a tuple or a list of 2 floats mentioning the width and height of the figure respectively. By default it is set as [6.4, 4.8]
dpi: As the name suggest, it takes in the dots per inch as a float value. Default value is 100.
facecolor : It is used to set the background color of the figure. Default value is ‘white’.
edgecolor: It is used to set the border color of the figure. Default value is ‘white’.
FigureClass: Takes in the subclass of Figure Optionally used for referring any custom Figure instance.
clear: used to clear any existing figure on the respective instance.
Return: instance.close()
It is recommended to use the .close() method after using figure to use a clean the unnecessary memory. so to close the figure instance, write.
Example 1: Extract a Figure instance using a figure()
The example mentioned below shows how the figure method is used to label any plot and using this same label as a reference point to fetch the Figure instance. The asset statement confirms the fact that both instances are pointing to the same reference point.
Python3
import matplotlib.pyplot as plt
fig = plt.figure( num = 'label' )
fig.get_label()
fig2 = plt.figure( num = 'label' )
assert fig = = fig2
|
Example 2: Plotting graph with custom height, width, and background
This example tells about how to plot a graph using the custom size of any plot and using custom dpi along with it. Here the background is also changed to yellow from white. The height is set to 10 and width to 7.
Python3
import matplotlib.pyplot as plt
plt.figure(num = 'label' ,
facecolor = 'yellow' ,
figsize = [ 10 , 7 ],
dpi = 50 )
plt.plot([ 2.5 , 1 , 2.5 , 4 , 2.5 ],
[ 1 , 2.5 , 4 , 2.5 , 1 ])
|
Output

Yellow background
Example 3: Example to clear the graph
The first code here is to show how the code will if two different plots are made on a single instance without using clear. The ideal thing that will work here is that it will plot both of them on a single figure.
Python3
import matplotlib.pyplot as plt
plt.plot([ 2.5 , 1 , 2.5 , 4 , 2.5 ],
[ 1 , 2.5 , 4 , 2.5 , 1 ])
plt.plot([ 1 , 2 , 3 , 4 ], [ 1 , 2 , 3 , 4 ])
|
Output

Both plotted
Now execute the same code but by clearing the first plot just before implementing the 2nd one.
Python3
import matplotlib.pyplot as plt
plt.plot([ 2.5 , 1 , 2.5 , 4 , 2.5 ],
[ 1 , 2.5 , 4 , 2.5 , 1 ])
plt.figure(clear = True )
plt.plot([ 1 , 2 , 3 , 4 ], [ 1 , 2 , 3 , 4 ])
|
Output

Two different plots on clearing
Example 4: Check the return type
The figure() returns a Figure instance, the next example just verifies this fact.
Python3
import matplotlib.pyplot as plt
print ( type (plt.figure()))
|
Output:

Similar Reads
Matplotlib.figure.Figure.ginput() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read
Matplotlib.figure.Figure.get_size_inches() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all 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
Matplotlib.figure.Figure.delaxes() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read
How to change axes limits in matplotlib?
Sometimes, when you create a plot default axes X and Y may not show the full picture you want. Changing the axes limits helps you focus on specific data ranges or improve how your plot looks. There are two methods available in Axes module tochange the limits: matplotlib.axes.Axes.set_xlim(): It is u
2 min read
How to Change Line Color in Matplotlib?
Matlab's plotting functions are included in Python by the Inclusion of the library Matplotlib. The library allows the plotting of the data of various dimensions without ambiguity in a plot. The library is widely used in Data Science and Data visualization. In this article, we will discuss how to cha
3 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 plo
2 min read
How to plot a dashed line in matplotlib?
Matplotlib is used to create visualizations and plotting dashed lines is used to enhance the style and readability of graphs. A dashed line can represent trends, relationships or boundaries in data. Below we will explore how to plot and customize dashed lines using Matplotlib. To plot dashed line: S
2 min read
Matplotlib.figure.Figure() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read
Matplotlib.figure.Figure.figimage() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read