Open In App

Matplotlib – Plot zooming with scroll wheel

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a Python library for data visualization and plotting, and MPL Interactions is a Python library for providing useful methods for interacting with Matplotlib plots. In this article, we will use MPL Interactions and the Matplotlib library to plot a graph that can be zoomed in with the mouse wheel. If Matplotlib and MPL Interactions are not installed on your system, please install them before plotting the graphs.

Required Packages

In the command prompt or terminal install the Matplotlib and MPL Interactions library using the following command:

pip install matplotlib
pip install mpl-interactions

Approaches

Below are approaches that we followed to Plot a graph with the ability to zoom with the scroll wheel.

  • Importing necessary library.
  • Create the dataset or you can load your own dataset for plotting the graph.
  • Enable scroll to zoom with the help of MPL Interactions library functions like off and zoom_factory.
  • Enable scrolling and panning with the help of the MPL Interactions library function like a panhandler.

Plot a Graph that can be Zoomed in with the Scroll Wheel

Importing necessary library. Create the dataset or you can load your own dataset for plotting the graph. Enable scroll to zoom with the help of MPL Interactions library functions like off and zoom_factory. Enable scrolling and panning with the help of the MPL Interactions library function like a panhandler.

Python3




# Importing required library
from mpl_interactions import ioff, panhandler, zoom_factory
import matplotlib.pyplot as plt
%matplotlib widget
 
# creating the dataset
data = {'Operating System': 10, 'Data Structure': 7,
        'Machine Learning': 14, 'Deep Learning': 12}
courses = list(data.keys())
values = list(data.values())
# Enable scroll to zoom with the help of MPL
# Interactions library function like ioff and zoom_factory.
with plt.ioff():
    figure, axis = plt.subplots()
# creating the bar plot
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.bar(courses, values, color='green', width=0.4)
disconnect_zoom = zoom_factory(axis)
# Enable scrolling and panning with the help of MPL
# Interactions library function like panhandler.
pan_handler = panhandler(figure)
display(figure.canvas)


Output:

Plot zooming with scroll wheel

 

Enable Zoomed-in with the Scroll Wheel using ScrollZoom 

Importing necessary library. Create the dataset or you can load your own dataset for plotting the graph. Plotting Graph for Created Dataset or Loaded Dataset Enable zoomed-in with the scroll wheel by setup the config={‘scrollZoom’: True} in the figure configuration.

Python3




# Importing library
import plotly.express as px
import pandas as pd
# initialise data of lists.
data = {'Subject': ["Data Analysis", "Data Science",
                    "Machine Learning", "Data Structure",
                    "Web Design", "Android Development"],
        'Total Student': [5, 10, 15, 20, 25, 30],
        'Male Student': [3, 4, 7, 10, 5, 17],
        'Female Student': [2, 6, 8, 10, 20, 13]}
 
# Create DataFrame
df = pd.DataFrame(data)
 
# Plotting Bar Plot for created Dataset With
# Enabled Scroll Zoom
plot = px.bar(df, x="Subject", y="Total Student",
              title="Plot With Enabled Scroll Zoom",
              color="Subject")
 
# Showing Scatter Plot with Scroll Zoom.
plot.show(config={'scrollZoom': True})


Output:

Plot zooming with scroll wheel

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads