Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82a2ab3

Browse files
author
root
committed
Added support for lists of string literals as arbitrary data tooltips
1 parent 3f78f05 commit 82a2ab3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,34 @@ def mouse_move(self, event):
30293029
str(newY) +
30303030
" Original coords: "
30313031
))
3032+
elif type(hover) == list:
3033+
import matplotlib.pyplot as plt
3034+
#get number of data points first
3035+
lines = plt.gca().get_lines()
3036+
num_of_points =0
3037+
for line in lines:
3038+
num_of_points+=1
3039+
if num_of_points >= len(hover):
3040+
raise ValueError("""Number of data points
3041+
does not match up woth number of labels""")
3042+
else:
3043+
mouse_x = event.xdata
3044+
mouse_y = event.ydata
3045+
for line in lines:
3046+
x_data = line.get_xdata()
3047+
y_data = line.get_ydata()
3048+
for i in range(len(x_data)):
3049+
# calculate distance between cursor position and data point
3050+
distance = ((event.xdata - x_data[i])**2 + (event.ydata - y_data[i])**2)**0.5
3051+
if distance < 0.05: # modify this threshold as needed
3052+
(self.set_hover_message("Data Label: "+ hover[i] +
3053+
" Original coords: "
3054+
))
3055+
3056+
3057+
3058+
3059+
30323060
else:
30333061
self.set_hover_message(self._mouse_event_to_message(event))
30343062
else:

lib/matplotlib/tests/test_toolkit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ def user_defined_function(event):
2222

2323
ax.plot(rand(100), 'o', hover=user_defined_function)
2424
plt.show()
25+
26+
27+
28+
############################################################################
29+
30+
#Alternative test for testing out string literals as tooltips
31+
32+
# fig, ax = plt.subplots()
33+
# plt.ylabel('some numbers')
34+
35+
# ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona'])
36+
# plt.show()

0 commit comments

Comments
 (0)