Matplotlib - Toolkits



A toolkit consists of a set of tools for a specific purpose. It contains different resources or software that work together to help you achieve a task. It is like having a handy package with everything you need to get a job done efficiently. Toolkits are often used in various fields, like technology, education, or research.

Toolkits in Matplotlib

Matplotlib toolkits are additional sets of tools and functions that you can use to extend the capabilities of Matplotlib. These toolkits are like add-ons that give you extra features for creating specific types of plots or addressing certain requirements.

For example, if you want to create 3D plots, you can use the "mplot3d" toolkit that comes with Matplotlib. It provides functions and classes specifically designed for 3D plotting.

Let us explore various toolkits in this tutorial −

The mpl_toolkits.mplot3d (3D Plotting)

The mpl_toolkits.mplot3d toolkit is used to create three-dimensional plots, including 3D scatter plots, surface plots, and wireframe plots. To use this toolkit, import a module called "Axes3D" −

from mpl_toolkits.mplot3d import Axes3D

Example

In the following example, we are creating a 3D scatter plot using the "mpl_toolkits.mplot3d" toolkit. We use the scatter() function to plot points in a 3D space, where the "x", "y", and "z" lists represent the coordinates of the data points −

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Creating a 3D scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y, z = [1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [2, 3, 4, 5, 6]
ax.scatter(x, y, z)

# Customizing the plot
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.set_title('3D Scatter Plot')

plt.show()

Output

Following is the output of the above code −

mplot3d Toolkit

The mpl_toolkits.axes_grid: Axes Grid

The mpl_toolkits.axes_grid toolkit is a set of tools that helps you create complex layouts of subplots and arrange custom axes in Matplotlib. To use this toolkit, import a specific module called "ImageGrid" −

from mpl_toolkits.axes_grid1 import ImageGrid

Example

Here, we are creating a grid of subplots (2 rows and 2 columns) using the "mpl_toolkits.axes_grid1" toolkit. The for loop iterates over each subplot in the grid, and a simple line plot is added to each subplot using the plot() function −

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid

# Creating a grid of subplots
fig = plt.figure(figsize=(6, 4))
grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1,)

# Plotting on each subplot
for ax in grid:
    ax.plot([1, 2, 3, 4], [5, 6, 7, 8])

plt.show()

Output

The resulting visualization displays a 2x2 grid of subplots, each containing a line plot as shown below −

axes_grid Toolkit

The mpl_toolkits.axisartist (Custom Axes)

The mpl_toolkits.axisartist toolkit offers additional features for creating customized axes and tick arrangements. To use this toolkit, import the module "Subplot" −

from mpl_toolkits.axisartist import Subplot

Example

Now, we are creating a custom subplot with enhanced axes using the "mpl_toolkits.axisartist" toolkit. We then use the plot() function to add a simple line plot to the custom subplot −

import matplotlib.pyplot as plt
from mpl_toolkits.axisartist import Subplot

# Creating a custom subplot with enhanced axes
fig = plt.figure()
ax = Subplot(fig, 111)
fig.add_subplot(ax)

# Plotting on the custom subplot
ax.plot(range(10))

# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

axisartist Toolkit

The mpl_toolkits.axes_grid1.inset_locator

The mpl_toolkits.axes_grid1.inset_locator enables the placement of inset axes within a larger plot, allowing for detailed views. To use this toolkit, import the module "inset_axes" −

from mpl_toolkits.axes_grid1.inset_locator import inset_axes

Example

In the following example, we are creating a main plot and an inset plot using the "mpl_toolkits.axes_grid1.inset_locator" toolkit. We use the inset_axes() function to define the inset axes, specifying its width, height, and location. Thereafter, we populate both the main plot and the inset plot with simple line plots using the plot() function −

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

# Creating a main plot
fig, ax = plt.subplots()

# Defining the inset axes
axins = inset_axes(ax, width='30%', height='30%', loc='upper right')

# Plotting on both axes
ax.plot(range(10))
axins.plot(range(5))

# Displaying the plot
plt.show()

Output

We get the output as shown below −

inset_locator Toolkit
Advertisements