From 9c6e817c618c81bb5ba71cd933ff300804534958 Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Mon, 17 Jun 2019 13:51:17 -0500 Subject: [PATCH 1/4] Fix ImageInspectorTool event key. --- chaco/tools/image_inspector_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chaco/tools/image_inspector_tool.py b/chaco/tools/image_inspector_tool.py index 6cae66da2..7e06a4ec4 100644 --- a/chaco/tools/image_inspector_tool.py +++ b/chaco/tools/image_inspector_tool.py @@ -75,7 +75,7 @@ def normal_mouse_move(self, event): else: self.new_value = \ {"indices": ndx, - "color_value": image_data.data[y_index, x_index]} + "data_value": image_data.data[y_index, x_index]} self.last_mouse_position = (event.x, event.y) return From 03eb32faa4f6bdbefc99fd9c247067d987ae09fd Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Mon, 17 Jun 2019 13:59:28 -0500 Subject: [PATCH 2/4] Code improvement. --- chaco/tools/image_inspector_tool.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/chaco/tools/image_inspector_tool.py b/chaco/tools/image_inspector_tool.py index 7e06a4ec4..76ea2ab57 100644 --- a/chaco/tools/image_inspector_tool.py +++ b/chaco/tools/image_inspector_tool.py @@ -64,19 +64,14 @@ def normal_mouse_move(self, event): x_index, y_index = ndx image_data = plot.value + new_value = {"indices": ndx, + "data_value": image_data.data[y_index, x_index]) if hasattr(plot, "_cached_mapped_image") and \ plot._cached_mapped_image is not None: - self.new_value = \ - {"indices": ndx, - "data_value": image_data.data[y_index, x_index], - "color_value": plot._cached_mapped_image[y_index, - x_index]} - - else: - self.new_value = \ - {"indices": ndx, - "data_value": image_data.data[y_index, x_index]} + new_value["color_value"] = \ + plot._cached_mapped_image[y_index, x_index] + self.new_value = new_value self.last_mouse_position = (event.x, event.y) return From 50b5d05b99ffe836d29354b9b2cb9bdefab4d2c9 Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Mon, 17 Jun 2019 14:06:09 -0500 Subject: [PATCH 3/4] Fix bug and update tests. --- .../demo/basic/dataframe_scatter_inspector.py | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 examples/demo/basic/dataframe_scatter_inspector.py diff --git a/examples/demo/basic/dataframe_scatter_inspector.py b/examples/demo/basic/dataframe_scatter_inspector.py new file mode 100644 index 000000000..c55d43bc1 --- /dev/null +++ b/examples/demo/basic/dataframe_scatter_inspector.py @@ -0,0 +1,71 @@ +""" Overlay to display the data attached to a scatter point hovered over. +""" +import pandas as pd +import numpy as np + +from traits.api import Callable, Enum, HasTraits, Instance, on_trait_change, \ + Str +from traitsui.api import View, Item +from enable.api import ComponentEditor +from chaco.api import Plot, ArrayPlotData, ScatterInspectorOverlay +from chaco.tools.api import ScatterInspector +from chaco.overlays.api import DataFrameScatterOverlay, TextBoxOverlay + + +def _create_plot_component(): + # Create a fake dataset from which 2 dimensions will be displayed in a + # scatter plot: + x = np.random.uniform(0.0, 10.0, 50) + y = np.random.uniform(0.0, 5.0, 50) + data = pd.DataFrame({"x": x, "y": y, + "dataset": np.random.choice(list("abcdefg"), 50)}) + plot_data = ArrayPlotData(x=x, y=y) + plot = Plot(plot_data) + scatter = plot.plot(("x", "y"), type="scatter")[0] + + # Attach the inspector and its overlays + inspector = ScatterInspector(component=scatter) + scatter.tools.append(inspector) + + text_overlay = DataframeScatterOverlay(component=plot, + inspector=inspector, + source_df=data, + bgcolor="black", alpha=0.6, + text_color="white", + border_color='none') + plot.overlays.append(text_overlay) + + # Optional: add an overlay on the point to confirm what is hovered over + # Note that this overlay magically knows about hovered points by + # listening to renderer events rather than inspector events: + point_overlay = ScatterInspectorOverlay(component=scatter, + hover_color="red", + hover_marker_size=6) + scatter.overlays.append(point_overlay) + return plot + + +# ============================================================================= +# Demo class that is used by the demo.py application. +# ============================================================================= + +size = (900, 500) +title = "Tooltip demo" + + +class Demo(HasTraits): + plot = Instance(Plot) + + traits_view = View( + Item('plot', editor=ComponentEditor(size=size), show_label=False), + resizable=True, title=title + ) + + def _plot_default(self): + return _create_plot_component() + + +demo = Demo() + +if __name__ == "__main__": + demo.configure_traits() From 83a3afeeb349e06abfd36d0559e60aeb63520c8a Mon Sep 17 00:00:00 2001 From: Jonathan Rocher Date: Mon, 17 Jun 2019 14:11:48 -0500 Subject: [PATCH 4/4] Fix bug. --- chaco/tools/image_inspector_tool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chaco/tools/image_inspector_tool.py b/chaco/tools/image_inspector_tool.py index 76ea2ab57..ee40a800a 100644 --- a/chaco/tools/image_inspector_tool.py +++ b/chaco/tools/image_inspector_tool.py @@ -31,7 +31,7 @@ class ImageInspectorTool(BaseTool): # Stores the value of self.visible when the mouse leaves the tool, # so that it can be restored when the mouse enters again. - _old_visible = Enum(None, True, False) #Trait(None, Bool(True)) + _old_visible = Enum(None, True, False) def normal_key_pressed(self, event): if self.inspector_key.match(event): @@ -65,7 +65,7 @@ def normal_mouse_move(self, event): x_index, y_index = ndx image_data = plot.value new_value = {"indices": ndx, - "data_value": image_data.data[y_index, x_index]) + "data_value": image_data.data[y_index, x_index]} if hasattr(plot, "_cached_mapped_image") and \ plot._cached_mapped_image is not None: new_value["color_value"] = \