1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
"""
Displaying a custom label for each individual point
===================================================
mpldatacursor's *point_labels* functionality can be emulated with an event
handler that sets the annotation text with a label selected from the target
index.
"""
import matplotlib.pyplot as plt
import mplcursors
import numpy as np
labels = ["a", "b", "c", "d", "e"]
x = np.array([0, 1, 2, 3, 4])
fig, ax = plt.subplots()
line, = ax.plot(x, x, "ro")
mplcursors.cursor(ax).connect(
"add", lambda sel: sel.annotation.set_text(labels[sel.index]))
plt.show()
|