I want to show annotations on a 3D scatter plot when the user clicks on a point.
The code I have shows the annotation once I move the plot after I click on a point.
JavaScript
x
28
28
1
from mpl_toolkits.mplot3d import proj3d
2
import matplotlib.pyplot as plt
3
4
fig = plt.figure()
5
ax = fig.add_subplot(111, projection = '3d')
6
7
x = [1, 2, 3]
8
y = [1, 2, 3]
9
z = [1, 2, 3]
10
11
scatter = ax.scatter(x,y,z,picker=True)
12
13
def annotate_onclick(event):
14
15
point_index = int(event.ind)
16
print(point_index)
17
18
proj = ax.get_proj()
19
20
x_p, y_p, _ = proj3d.proj_transform(x[point_index], y[point_index], z[point_index], proj)
21
22
plt.annotate(str(point_index), xy=(x_p, y_p))
23
24
25
fig.canvas.mpl_connect('pick_event', annotate_onclick)
26
27
plt.show()
28
How can I make the annotation appear as soon as the user clicks on a point, without having to move the plot?
Advertisement
Answer
Add fig.canvas.draw_idle()
at the end of your callback function to force the re-drawing of the new annotation.