79576306

Date: 2025-04-16 02:44:50
Score: 0.5
Natty:
Report link

Matplotlib does not offer a built-in function in its core library to enable hover effects. For this functionality, you may consider using the mplcursors library. Kindly try running the code below after installing mplcursors.

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

x = np.random.rand(20)
y = np.random.rand(20)
colors = np.random.rand(20)
area = (30 * np.random.rand(20))**2

metadata = [f"Point {i}, Value: ({x[i]:.2f}, {y[i]:.2f})" for i in range(len(x))]

fig, ax = plt.subplots()
scatter = ax.scatter(x, y, s=area, c=colors, alpha=0.5)

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Interactive Scatter Plot')

cursor = mplcursors.cursor(scatter, hover=True)

@cursor.connect("add")
def on_add(sel):
    sel.annotation.set_text(metadata[sel.index])

plt.show()

Output:

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Subir Chowdhury