Signup/Sign In
LAST UPDATED: SEPTEMBER 8, 2020

Matplotlib Figure Class

In this tutorial, we will cover the Figure class in the matplotlib library.

As we all know that matplotlib is a python library used to create visualization which is also a numerical extension of Numpy library.

  • The matplotlib library contains a Figure class in the matplotlib.figure module.

  • For all plotting elements figure class is mainly a top-level container.

  • To instantiate the figure object you just need to call the figure() function of the pyplot module ( It is a state-based interface to matplotlib)

  • With the help of pyplot one can easily create histograms, violin plot, contour plot, 3D plots, and many more.

  • We can also extend the Figure class and create a custom class based on our requirements. (Python Inheritance)

figure() Function

To create a new figure the figure() function of the pyplot module is used in the matplotlib library.

Syntax:

The syntax to use this function is given below:

matplotlib.pyplot.figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, clear, **kwargs)

Parameters:

Let us discuss the parameters of the figure() function:

1. num

The default value of this parameter is None. You can provide this parameter and also the figure with this id already exists.

2. figsize(float, float)

This is used to indicate the width and height in pixels.

3. dpi

This parameter is used to indicate the resolution of the figure. The default value is None.

4. facecolor

This parameter is used to indicate the background color.

5. edgecolor

This parameter is used to indicate the border color.

6. frameon

If you do not want to draw the frame of the figure, this option is used. The default value of this parameter is true, which means by default the frame is drawn.

7. FigureClass

This parameter mainly uses a custom Figure instance.

8. clear

The default value is false. If the value of this parameter is true and the figure already exists, then it is cleared.

Returned Values:

This method returns the Figure instance which is also passed to new_figure_manager in the backend.

Time for Example:

Let us discuss this function with the help of an example given below:

In the above code example, we have created a new class MyFigure inheriting the original Figure class.

Summary:

In this tutorial, we covered the Figure class available in the matplotlib.figure module. We also learned about the figure() function which is very useful.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.