diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 06fbc8106a54..46e115bba203 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3029,6 +3029,30 @@ def mouse_move(self, event): str(newY) + " Original coords: " )) + elif type(hover) == list: + import matplotlib.pyplot as plt + lines = plt.gca().get_lines() + num_of_points = 0 + for line in lines: + num_of_points += 1 + if num_of_points >= len(hover): + raise ValueError("""Number of data points + does not match up woth number of labels""") + else: + mouse_x = event.xdata + mouse_y = event.ydata + for line in lines: + x_data = line.get_xdata() + y_data = line.get_ydata() + for i in range(len(x_data)): + distance = ((event.xdata - x_data[i])**2 + + (event.ydata - y_data[i])**2)**0.5 + if distance < 0.05: + (self.set_hover_message("Data Label: " + + hover[i] + + " " + + "Original coords: " + )) else: self.set_hover_message(self._mouse_event_to_message(event)) else: diff --git a/lib/matplotlib/tests/test_toolkit.py b/lib/matplotlib/tests/test_toolkit.py index 8b40a120c781..a5f57b0f9ec1 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -13,12 +13,20 @@ # print(matplotlib.__version__, matplotlib.__file__) -fig, ax = plt.subplots() -plt.ylabel('some numbers') +# fig, ax = plt.subplots() +# plt.ylabel('some numbers') + + +# def user_defined_function(event): +# return round(event.xdata * 10, 1), round(event.ydata + 3, 3) +# ax.plot(rand(100), 'o', hover=user_defined_function) +# plt.show() -def user_defined_function(event): - return round(event.xdata * 10, 1), round(event.ydata + 3, 3) +# Alternative test for testing out string literals as tooltips: + +fig, ax = plt.subplots() +plt.ylabel('some numbers') -ax.plot(rand(100), 'o', hover=user_defined_function) +ax.plot(rand(3), 'o', hover=['London', 'Paris', 'Barcelona']) plt.show()