mplcursors could be an interesting approach. In the connected function you can show an annotation, but also e.g. update the statusbar or write something in the console.
import matplotlib.pyplot as plt
import mplcursors
prev = None
fig, ax = plt.subplots()
ax.bar(range(9), range(1, 10), align="center")
ax.set(xticks=range(9), xticklabels=[*'ABCDEFGHI'], title="Hover over a bar")
cursor = mplcursors.cursor(hover=True)
@cursor.connect("add")
def on_add(sel):
global prev
x, y, width, height = sel.artist[sel.target.index].get_bbox().bounds
sel.annotation.set(text=f"{sel.target.index + 1}: {height}",
position=(0, 20), anncoords="offset points")
sel.annotation.xy = (x + width / 2, y + height)
bar_num = sel.target.index + 1
postfix = "st" if bar_num == 1 else "nd" if bar_num == 2 else "rd" if bar_num == 3 else "th"
if bar_num != prev:
print(f"You hovered over the {bar_num}{postfix} bar.")
prev = bar_num
plt.show()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…