Interactive Data Visualization with Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In data visualization, zooming in on a specific area of a plot can be extremely useful to better understand and analyze the data. Matplotlib, a popular data visualization library for Python, provides a way to create two identical panels and zoom in on the right panel to show a rectangle in the first panel, denoting the zoomed region. In this lab, we will learn how to create this interactive zooming feature in Matplotlib.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} matplotlib/figures_axes -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} matplotlib/heatmaps -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} matplotlib/event_handling -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/variables_data_types -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/numeric_types -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/booleans -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/for_loops -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/lists -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/tuples -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/function_definition -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/default_arguments -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/importing_modules -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/using_packages -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/classes_objects -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/constructor -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/polymorphism -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/encapsulation -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/numerical_computing -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/data_visualization -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} python/build_in_functions -.-> lab-49022{{"`Interactive Data Visualization with Matplotlib`"}} end

Import necessary libraries

We will first import the necessary libraries, including Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.patches import Rectangle

Create the UpdatingRect class

We will create a subclass of Rectangle called UpdatingRect. This class is called with an Axes instance, causing the rectangle to update its shape to match the bounds of the Axes.

class UpdatingRect(Rectangle):
    def __call__(self, ax):
        self.set_bounds(*ax.viewLim.bounds)
        ax.figure.canvas.draw_idle()

Create the MandelbrotDisplay class

We will create a class called MandelbrotDisplay that will regenerate a fractal set as we zoom in, so that we can actually see the increasing detail. A box in the left panel will show the area to which we are zoomed.

class MandelbrotDisplay:
    def __init__(self, h=500, w=500, niter=50, radius=2., power=2):
        self.height = h
        self.width = w
        self.niter = niter
        self.radius = radius
        self.power = power

    def compute_image(self, xstart, xend, ystart, yend):
        self.x = np.linspace(xstart, xend, self.width)
        self.y = np.linspace(ystart, yend, self.height).reshape(-1, 1)
        c = self.x + 1.0j * self.y
        threshold_time = np.zeros((self.height, self.width))
        z = np.zeros(threshold_time.shape, dtype=complex)
        mask = np.ones(threshold_time.shape, dtype=bool)
        for i in range(self.niter):
            z[mask] = z[mask]**self.power + c[mask]
            mask = (np.abs(z) < self.radius)
            threshold_time += mask
        return threshold_time

    def ax_update(self, ax):
        ax.set_autoscale_on(False)
        self.width, self.height = \
            np.round(ax.patch.get_window_extent().size).astype(int)
        vl = ax.viewLim
        extent = vl.x0, vl.x1, vl.y0, vl.y1
        im = ax.images[-1]
        im.set_data(self.compute_image(*extent))
        im.set_extent(extent)
        ax.figure.canvas.draw_idle()

Create the plot

We will create the plot by first computing the image using the MandelbrotDisplay class, and then creating two identical panels using subplots. We will add the image to both panels using imshow, and add the UpdatingRect object to the left panel.

md = MandelbrotDisplay()
Z = md.compute_image(-2., 0.5, -1.25, 1.25)

fig1, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(Z, origin='lower',
           extent=(md.x.min(), md.x.max(), md.y.min(), md.y.max()))
ax2.imshow(Z, origin='lower',
           extent=(md.x.min(), md.x.max(), md.y.min(), md.y.max()))

rect = UpdatingRect(
    [0, 0], 0, 0, facecolor='none', edgecolor='black', linewidth=1.0)
rect.set_bounds(*ax2.viewLim.bounds)
ax1.add_patch(rect)

Add zooming functionality

We will add the zooming functionality by connecting the xlim_changed and ylim_changed events to the UpdatingRect and MandelbrotDisplay objects.

ax2.callbacks.connect('xlim_changed', rect)
ax2.callbacks.connect('ylim_changed', rect)

ax2.callbacks.connect('xlim_changed', md.ax_update)
ax2.callbacks.connect('ylim_changed', md.ax_update)
ax2.set_title("Zoom here")

Show the plot

We will show the plot using the show() function.

plt.show()

Summary

In this lab, we learned how to create an interactive zooming feature in Matplotlib using two identical panels and the UpdatingRect and MandelbrotDisplay classes. By adding zooming functionality, we can better understand and analyze the data in our plots.

Other Python Tutorials you may like