From 82a2ab392579981f56529d45c05a621f203688c7 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 08:10:09 -0400 Subject: [PATCH 1/8] Added support for lists of string literals as arbitrary data tooltips --- lib/matplotlib/backend_bases.py | 28 ++++++++++++++++++++++++++++ lib/matplotlib/tests/test_toolkit.py | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 06fbc8106a54..e4ae3604c873 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3029,6 +3029,34 @@ def mouse_move(self, event): str(newY) + " Original coords: " )) + elif type(hover) == list: + import matplotlib.pyplot as plt + #get number of data points first + 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)): + # calculate distance between cursor position and data point + distance = ((event.xdata - x_data[i])**2 + (event.ydata - y_data[i])**2)**0.5 + if distance < 0.05: # modify this threshold as needed + (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..ae43d442ecbc 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -22,3 +22,15 @@ def user_defined_function(event): ax.plot(rand(100), 'o', hover=user_defined_function) plt.show() + + + +############################################################################ + +#Alternative test for testing out string literals as tooltips + +# fig, ax = plt.subplots() +# plt.ylabel('some numbers') + +# ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona']) +# plt.show() \ No newline at end of file From d6a1d3164bd3fcce86064e2d4c336562764a9c2c Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 16:05:53 -0400 Subject: [PATCH 2/8] initial style fixes --- lib/matplotlib/backend_bases.py | 18 ++++++------------ lib/matplotlib/tests/test_toolkit.py | 2 +- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index e4ae3604c873..9ff9822de692 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3031,13 +3031,12 @@ def mouse_move(self, event): )) elif type(hover) == list: import matplotlib.pyplot as plt - #get number of data points first lines = plt.gca().get_lines() - num_of_points =0 + num_of_points = 0 for line in lines: - num_of_points+=1 + num_of_points += 1 if num_of_points >= len(hover): - raise ValueError("""Number of data points + raise ValueError("""Number of data points does not match up woth number of labels""") else: mouse_x = event.xdata @@ -3046,17 +3045,12 @@ def mouse_move(self, event): x_data = line.get_xdata() y_data = line.get_ydata() for i in range(len(x_data)): - # calculate distance between cursor position and data point - distance = ((event.xdata - x_data[i])**2 + (event.ydata - y_data[i])**2)**0.5 - if distance < 0.05: # modify this threshold as needed + 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 ae43d442ecbc..f52806308346 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -33,4 +33,4 @@ def user_defined_function(event): # plt.ylabel('some numbers') # ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona']) -# plt.show() \ No newline at end of file +# plt.show() From bf83d3e0e77228204dc91f567f9d34e87bbbe067 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 16:48:03 -0400 Subject: [PATCH 3/8] further linter fixes --- lib/matplotlib/backend_bases.py | 5 +++-- lib/matplotlib/tests/test_toolkit.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 9ff9822de692..d347cdcc129c 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3045,10 +3045,11 @@ def mouse_move(self, event): x_data = line.get_xdata() y_data = line.get_ydata() for i in range(len(x_data)): - distance = ((event.xdata - x_data[i])**2 + 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] + + (self.set_hover_message("Data Label: " + + hover[i] + " Original coords: " )) else: diff --git a/lib/matplotlib/tests/test_toolkit.py b/lib/matplotlib/tests/test_toolkit.py index f52806308346..80ebac1bd4b6 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -25,9 +25,9 @@ def user_defined_function(event): -############################################################################ +############################################################################### -#Alternative test for testing out string literals as tooltips +# Alternative test for testing out string literals as tooltips # fig, ax = plt.subplots() # plt.ylabel('some numbers') From cfc7039493fc48b2aaae973b317fe4bb7b1f62d4 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 16:59:03 -0400 Subject: [PATCH 4/8] Even more linter fixes --- lib/matplotlib/backend_bases.py | 4 ++-- lib/matplotlib/tests/test_toolkit.py | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index d347cdcc129c..64ba8d3cbf74 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3048,9 +3048,9 @@ def mouse_move(self, event): 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: " + (self.set_hover_message("Data Label: " + hover[i] + - " Original coords: " + " Original coords: " )) else: self.set_hover_message(self._mouse_event_to_message(event)) diff --git a/lib/matplotlib/tests/test_toolkit.py b/lib/matplotlib/tests/test_toolkit.py index 80ebac1bd4b6..4ceedd1abc02 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -23,11 +23,7 @@ def user_defined_function(event): ax.plot(rand(100), 'o', hover=user_defined_function) plt.show() - - -############################################################################### - -# Alternative test for testing out string literals as tooltips +# Alternative test for testing out string literals as tooltips: # fig, ax = plt.subplots() # plt.ylabel('some numbers') From e2ae36ab9a2d48ed4a53caba42e65e5c6ebb5d20 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 17:32:44 -0400 Subject: [PATCH 5/8] linter fixes for backend_bases --- lib/matplotlib/backend_bases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 64ba8d3cbf74..486d77ef66d9 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3050,8 +3050,8 @@ def mouse_move(self, event): if distance < 0.05: (self.set_hover_message("Data Label: " + hover[i] + - " Original coords: " - )) + " Original coords: " + )) else: self.set_hover_message(self._mouse_event_to_message(event)) else: From 4b6fb180bac96fe8655e40601cd3da70998ef93d Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 17:39:39 -0400 Subject: [PATCH 6/8] More linter fixes for backend_bases --- lib/matplotlib/backend_bases.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 486d77ef66d9..10ae8e06456f 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3049,9 +3049,9 @@ def mouse_move(self, event): + (event.ydata - y_data[i])**2)**0.5 if distance < 0.05: (self.set_hover_message("Data Label: " - + hover[i] + - " Original coords: " - )) + + hover[i] + + " Original coords: " + )) else: self.set_hover_message(self._mouse_event_to_message(event)) else: From 824a398dbdd49a496de8d60fbee3a988a3eece9d Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 18:16:15 -0400 Subject: [PATCH 7/8] Should be the last push for linting --- lib/matplotlib/backend_bases.py | 7 ++++--- lib/matplotlib/tests/test_toolkit.py | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 10ae8e06456f..46e115bba203 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3049,9 +3049,10 @@ def mouse_move(self, event): + (event.ydata - y_data[i])**2)**0.5 if distance < 0.05: (self.set_hover_message("Data Label: " - + hover[i] + - " Original coords: " - )) + + 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 4ceedd1abc02..99684783b1da 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -13,20 +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) +# 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() +# ax.plot(rand(100), 'o', hover=user_defined_function) +# plt.show() # Alternative test for testing out string literals as tooltips: -# fig, ax = plt.subplots() -# plt.ylabel('some numbers') +fig, ax = plt.subplots() +plt.ylabel('some numbers') -# ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona']) -# plt.show() +ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona']) +plt.show() From 7aa05719cbf76d623f5075589579c0b49cf480de Mon Sep 17 00:00:00 2001 From: root Date: Sun, 7 May 2023 18:26:15 -0400 Subject: [PATCH 8/8] Should really be the last linter push now --- lib/matplotlib/tests/test_toolkit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_toolkit.py b/lib/matplotlib/tests/test_toolkit.py index 99684783b1da..a5f57b0f9ec1 100644 --- a/lib/matplotlib/tests/test_toolkit.py +++ b/lib/matplotlib/tests/test_toolkit.py @@ -28,5 +28,5 @@ fig, ax = plt.subplots() plt.ylabel('some numbers') -ax.plot(rand(3), 'o', hover=['London','Paris','Barcelona']) +ax.plot(rand(3), 'o', hover=['London', 'Paris', 'Barcelona']) plt.show()