Matplotlib Object Oriented Interface

In this tutorial, we will cover the object-oriented interface in Matplotlib.

Matplotlib Interfaces

There are two types of interfaces in Matplotlib for visualization and these are given below:

matplotlib Interfaces

1. MATLAB like interface Using Pyplot Interface

You can easily generate plots using pyplot module in the matplotlib library just by importing matplotlib.pyplot module.

  • Also pyplot interface is a state-based interface.

  • The main quality of state-based interface is that it allows us to add elements and/or modify the plot as we need, whenever we need it.

  • The Pyplot interface shares a lot of similarities in syntax and methodology with MATLAB.

There are some disadvantages too with this interface that's why Object Oriented interface of Matplotlib come into play. The pyplot interface doesn't really scale well when there is a need to make multiple plots or when we have to make plots that require a lot of customization.

2. Object Oriented Interface in Matplotlib

To get more control over the plots created using matplotlib and for more customization of plots we use object-oriented interface of matplotlib.

  • The Object-Oriented interface can be accessed so easily also allow us to reuse objects and matplotlib internally used this object-oriented interface.

  • The Object-Oriented approach is better at times when there is a need to draw Multiple plots on the canvas.

  • The ides behind this interface is to create figure objects and then just need to call methods or attributes off of that object.

  • In an object-oriented interface in matplotlib, Pyplot is used only for a few functions like figure creation, and also the user explicitly creates the figure and keeps track of the figure and axes objects.

  • In this the user makes use of Pyplot to create figures, and with the help of those figures, one or more axes objects can also be created. You can use axes objects for most plotting actions.

Figure is mainly divided into two parts in matplotlib:

1. The Figure object:

The Figure object contains one or more axes objects.

Let us try to run some code:

import matplotlib.pyplot as plt
fig = plt.figure()

The output for the same will be as follows:

<class 'matplotlib.figure.Figure'>

2. The Axes object:

An axes represents a plot inside the figure

Let us try some code given below for the axes object:

from matplotlib import pyplot as plt
import numpy as np
import math
x = np.arange(0, math.pi*2, 0.05)
y = np.cos(x)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_title("cos wave")# used to set the title
ax.set_xlabel('angle')# used to set label for x-axis
ax.set_ylabel('cos') #used to set label for y-axis # used to display the plot

Description of the functions used in the above code:

1. plt.figure()

This function will create a figure instance that provides an empty canvas.

2. fig.add_axes([0,0,1,1])

The add_axes() method requires a list object of 4 elements that are corresponding to the left, bottom, width, and height of the figure. It is important to note that Each number must lie in between 0 and 1.

The output of the above code is as follows:


In this tutorial, we have covered the various interfaces and class of Matplotlib module.