0
0
Matplotlibdata~30 mins

Mplcursors for hover labels in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Interactive Hover Labels with Mplcursors in Matplotlib
📖 Scenario: You are analyzing sales data for a small store. You want to create a simple scatter plot of sales over days and add interactive hover labels to see exact values when you move the mouse over points.
🎯 Goal: Create a scatter plot using matplotlib and add interactive hover labels using mplcursors to display the exact sales value when hovering over each point.
📋 What You'll Learn
Create a dictionary called sales_data with days as keys and sales as values
Create a list called days containing the keys from sales_data
Create a list called sales containing the values from sales_data
Plot a scatter plot of days vs sales using matplotlib.pyplot.scatter
Use mplcursors.cursor to add hover labels showing sales values
Print the plot with interactive hover labels
💡 Why This Matters
🌍 Real World
Interactive plots help analysts explore data visually and get exact values easily without cluttering the graph.
💼 Career
Data scientists and analysts often use interactive visualizations to communicate insights clearly and allow users to explore data points.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 1: 150, 2: 200, 3: 170, 4: 220, 5: 180
Matplotlib
Need a hint?

Use curly braces {} to create a dictionary with keys as days and values as sales.

2
Extract days and sales lists
Create a list called days containing the keys from sales_data and a list called sales containing the values from sales_data
Matplotlib
Need a hint?

Use list() with sales_data.keys() and sales_data.values() to get the lists.

3
Plot scatter and add mplcursors hover labels
Import matplotlib.pyplot as plt and mplcursors. Plot a scatter plot of days vs sales using plt.scatter(days, sales). Then use mplcursors.cursor() on the scatter plot to add hover labels showing sales values.
Matplotlib
Need a hint?

Use plt.scatter() to plot points and mplcursors.cursor() with hover=True to enable hover labels.

4
Display the plot with hover labels
Add plt.show() to display the scatter plot with interactive hover labels.
Matplotlib
Need a hint?

Use plt.show() to open the plot window with interactive hover labels.