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

Skip to content

Commit 5c5e5cc

Browse files
authored
Merge pull request #6 from eslothower/task3
Added support for lists of string literals as arbitrary data tooltips
2 parents b7229c5 + 7aa0571 commit 5c5e5cc

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/matplotlib/backend_bases.py

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

lib/matplotlib/tests/test_toolkit.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313

1414
# print(matplotlib.__version__, matplotlib.__file__)
1515

16-
fig, ax = plt.subplots()
17-
plt.ylabel('some numbers')
16+
# fig, ax = plt.subplots()
17+
# plt.ylabel('some numbers')
18+
19+
20+
# def user_defined_function(event):
21+
# return round(event.xdata * 10, 1), round(event.ydata + 3, 3)
1822

23+
# ax.plot(rand(100), 'o', hover=user_defined_function)
24+
# plt.show()
1925

20-
def user_defined_function(event):
21-
return round(event.xdata * 10, 1), round(event.ydata + 3, 3)
26+
# Alternative test for testing out string literals as tooltips:
27+
28+
fig, ax = plt.subplots()
29+
plt.ylabel('some numbers')
2230

23-
ax.plot(rand(100), 'o', hover=user_defined_function)
31+
ax.plot(rand(3), 'o', hover=['London', 'Paris', 'Barcelona'])
2432
plt.show()

0 commit comments

Comments
 (0)