Matplotlib – Plot zooming with scroll wheel
Last Updated :
02 Jan, 2023
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
from mpl_interactions import ioff, panhandler, zoom_factory
import matplotlib.pyplot as plt
% matplotlib widget
data = { 'Operating System' : 10 , 'Data Structure' : 7 ,
'Machine Learning' : 14 , 'Deep Learning' : 12 }
courses = list (data.keys())
values = list (data.values())
with plt.ioff():
figure, axis = plt.subplots()
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)
pan_handler = panhandler(figure)
display(figure.canvas)
|
Output:
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
import plotly.express as px
import pandas as pd
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 ]}
df = pd.DataFrame(data)
plot = px.bar(df, x = "Subject" , y = "Total Student" ,
title = "Plot With Enabled Scroll Zoom" ,
color = "Subject" )
plot.show(config = { 'scrollZoom' : True })
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...