Creating a figure explicitly is an object-oriented style of interfacing with matplotlib. The figure is a basic building block of creating a plot as Matplotlib graphs our data on figures. This figure keeps track of all other components such as child axes, legends, title, axis, etc.
Steps to create an empty figure :
- First, we import the matplotlib library specifically the pyplot module of matplotlib.
- Then we create a figure object using plt.figure() and keep a reference to that by setting it equal to the ‘fig’ variable. This figure object is empty as we have not added any figure components such as axis, legend, axis, etc.
- We are using jupyter notebook, and we have to change out backend to ipympl(interactive backend) as with the default backend it was showing a Non-Gui backend error.
For installing the ipympl run this command into your terminal:
For conda environment.
conda install ipympl -c conda-forge
For normal python terminal:
pip install ipympl
Below is the implementation:
Example 2 :
You can also use another interactive backend to display your figure like TkAgg( requires TkInter installed).
Note: Displaying the figure in a different editor or python shell will need you to play with backends.
show() method also displays an empty figure but you have to save that figure before using show() command.
In the following example, I have used figsize attribute to change the size of the figure.
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