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.
from mpl_toolkits.mplot3d import proj3d import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection = '3d') x = [1, 2, 3] y = [1, 2, 3] z = [1, 2, 3] scatter = ax.scatter(x,y,z,picker=True) def annotate_onclick(event): point_index = int(event.ind) print(point_index) proj = ax.get_proj() x_p, y_p, _ = proj3d.proj_transform(x[point_index], y[point_index], z[point_index], proj) plt.annotate(str(point_index), xy=(x_p, y_p)) fig.canvas.mpl_connect('pick_event', annotate_onclick) plt.show()
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.