How to modify existing figure instance in Matplotlib?
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)
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.
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.
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.
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.
Now execute the same code but by clearing the first plot just before implementing the 2nd one.
Example 4: Check the return type
The figure() returns a Figure instance, the next example just verifies this fact.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course