Buy Me a Coffee? Your support is much appreciated!
demo.py
import matplotlib.pyplot as plt
import numpy as np
# Step 1. Create a scatter chart
x = np.random.rand(20)
y = np.random.rand(20)
colors = np.random.randint(1, 5, size=len(x))
norm = plt.Normalize(1, 4)
cmap = plt.cm.PiYG
fig, ax = plt.subplots()
scatter = plt.scatter(
x=x,
y=y,
c=colors,
s=100,
cmap=cmap,
norm=norm
)
# Step 2. Create Annotation Object
annotation = ax.annotate(
text='',
xy=(0, 0),
xytext=(15, 15), # distance from x, y
textcoords='offset points',
bbox={'boxstyle': 'round', 'fc': 'w'},
arrowprops={'arrowstyle': '->'}
)
annotation.set_visible(False)
# Step 3. Implement the hover event to display annotations
def motion_hover(event):
annotation_visbility = annotation.get_visible()
if event.inaxes == ax:
is_contained, annotation_index = scatter.contains(event)
if is_contained:
data_point_location = scatter.get_offsets()[annotation_index['ind'][0]]
annotation.xy = data_point_location
text_label = '({0:.2f}, {0:.2f})'.format(data_point_location[0], data_point_location[1])
annotation.set_text(text_label)
annotation.get_bbox_patch().set_facecolor(cmap(norm(colors[annotation_index['ind'][0]])))
annotation.set_alpha(0.4)
annotation.set_visible(True)
fig.canvas.draw_idle()
else:
if annotation_visbility:
annotation.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect('motion_notify_event', motion_hover)
plt.show()

Can these annotations be used with line chart? Below is the code and I can’t get it to work.
figure2 = plt.Figure(figsize=(5, 4), dpi=100)
ax2 = figure2.add_subplot(111)
line2 = FigureCanvasTkAgg(figure2, root)
line2.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df2 = df2[[‘year’, ‘unemployment_rate’]].groupby(‘year’).sum()
df2.plot(kind=’line’, legend=True, ax=ax2, color=’r’, marker=’o’, fontsize=10)
ax2.set_title(‘Year Vs. Unemployment Rate’)
# Step 2. Create Annotation Object
annotation = ax2.annotate(
text=”,
xy=(0, 0),
xytext=(15, 15), # distance from x, y
textcoords=’offset points’,
bbox={‘boxstyle’: ’round’, ‘fc’: ‘w’},
arrowprops={‘arrowstyle’: ‘->’}
)
annotation.set_visible(False)
# Step 3. Implement the hover event to display annotations
def motion_hover(event):
annotation_visbility = annotation.get_visible()
if event.inaxes == ax2:
is_contained, annotation_index = figure2.contains(event)
if is_contained:
data_point_location = figure2.get_offsets()[annotation_index[‘ind’][0]]
annotation.xy = data_point_location
text_label = ‘({0:.2f}, {0:.2f})’.format(data_point_location[0], data_point_location[1])
annotation.set_text(text_label)
annotation.get_bbox_patch().set_facecolor(cmap(norm(colors[annotation_index[‘ind’][0]])))
annotation.set_alpha(0.4)
annotation.set_visible(True)
figure2.canvas.draw_idle()
else:
if annotation_visbility:
annotation.set_visible(False)
figure2.canvas.draw_idle()
figure2.canvas.mpl_connect(‘motion_notify_event’, motion_hover)