Exemplo n.º 1
0
def _create_plot_component():  # Create a scalar field to colormap
    xbounds = (-2 * pi, 2 * pi, 600)
    ybounds = (-1.5 * pi, 1.5 * pi, 300)
    xs = linspace(*xbounds)
    ys = linspace(*ybounds)
    x, y = meshgrid(xs, ys)
    z = sin(x) * y

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("imagedata", z)

    # Create the plot
    plot = Plot(pd)
    img_plot = plot.img_plot("imagedata",
                             xbounds=xbounds[:2],
                             ybounds=ybounds[:2],
                             colormap=viridis)[0]

    # Tweak some of the plot properties
    plot.title = "Image Plot with Lasso"
    plot.padding = 50

    lasso_selection = LassoSelection(component=img_plot)
    lasso_selection.on_trait_change(lasso_updated, "disjoint_selections")
    lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                 component=img_plot)
    img_plot.tools.append(lasso_selection)
    img_plot.overlays.append(lasso_overlay)
    return plot
Exemplo n.º 2
0
    def _create_plot_component(self):
        """ Creates the plot component of the to be used in the FeatureScatter
            instance.
        """
        x = np.zeros(len(self.data))
        y = np.zeros(len(self.data))
        c = np.zeros(len(self.data))

        for i, (coord, count) in enumerate(self.data.items()):
            x[i], y[i] = coord
            c[i] = count

        c = np.log2(c)

        pd = ArrayPlotData()
        pd.set_data("x", x)
        pd.set_data("y", y)
        pd.set_data("color", c)

        cm = Map(DataRange1D(low=-c.max() / 2, high=c.max()))

        plot = Plot(pd)
        plot.plot(("x", "y", "color"),
                  type="cmap_scatter",
                  name="my_plot",
                  marker="dot",
                  index_sort="ascending",
                  color_mapper=cm,
                  marker_size=2,
                  bgcolor=0xF7F7F7,
                  )

        plot.title = "Scatter Plot With Lasso Selection"
        plot.line_width = 1
        plot.padding = 50

        my_plot = plot.plots["my_plot"][0]
        my_plot.data = self.data
        my_plot.out_file = self.out_file
        my_plot.label = self.label

        lasso_selection = FeatureLasso(component=my_plot,
                                       selection_datasource=my_plot.index,
                                       drag_button="left")

        my_plot.tools.append(lasso_selection)
        my_plot.tools.append(BetterZoom(my_plot, zoom_factor=1.2))
        my_plot.tools.append(PanTool(my_plot, drag_button="right"))
        my_plot.tools.append(ScatterInspector(my_plot))

        lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                     component=my_plot,
                                     selection_fill_color=0xEF8A62)

        my_plot.overlays.append(lasso_overlay)
        my_plot.overlays.append(ScatterInspectorOverlay(my_plot,
                                hover_marker_size=4))

        return plot
Exemplo n.º 3
0
    def _draw_component(self, gc, view_bounds=None, mode="normal"):
        LassoOverlay._draw_component(self, gc, view_bounds, mode)
        selection = self.lasso_selection

        if selection.fit_params is not None:
            # draw the label overlay
            self._label.component = self.component
            c = self.component

            if selection.fit_params[1] < 0:
                operator = "-"
            else:
                operator = "+"
            self._label.text = "%.2fx "%selection.fit_params[0] + operator + \
                               " %.2f" % fabs(selection.fit_params[1])
            w, h = self._label.get_width_height(gc)
            x = (c.x + c.x2) / 2 - w / 2
            y = c.y + 5  # add some padding on the bottom
            with gc:
                gc.translate_ctm(x, y)
                self._label.draw(gc)

            # draw the line
            slope, y0 = selection.fit_params
            f = lambda x: slope * x + y0
            cx, cy = c.map_screen([selection.centroid])[0]
            left = c.x
            right = c.x2

            left_x = c.map_data([left, c.y])[0]
            right_x = c.map_data([right, c.y])[0]
            left_y = f(left_x)
            right_y = f(right_x)

            left_pt, right_pt = c.map_screen([[left_x, left_y],
                                              [right_x, right_y]])

            with gc:
                gc.set_line_dash(self.line_style_)
                gc.set_stroke_color(self.line_color_)
                gc.set_line_width(self.line_width)
                gc.move_to(*left_pt)
                gc.line_to(*right_pt)
                gc.stroke_path()

        return
Exemplo n.º 4
0
    def _draw_component(self, gc, view_bounds=None, mode="normal"):
        LassoOverlay._draw_component(self, gc, view_bounds, mode)
        selection = self.lasso_selection

        if selection.fit_params is not None:
            # draw the label overlay
            self._label.component = self.component
            c = self.component

            if selection.fit_params[1] < 0:
                operator = "-"
            else:
                operator = "+"
            self._label.text = "%.2fx "%selection.fit_params[0] + operator + \
                               " %.2f" % fabs(selection.fit_params[1])
            w, h = self._label.get_width_height(gc)
            x = (c.x+c.x2)/2 - w/2
            y = c.y + 5  # add some padding on the bottom
            with gc:
                gc.translate_ctm(x, y)
                self._label.draw(gc)

            # draw the line
            slope, y0 = selection.fit_params
            f = lambda x: slope*x + y0
            cx, cy = c.map_screen([selection.centroid])[0]
            left = c.x
            right = c.x2

            left_x = c.map_data([left, c.y])[0]
            right_x = c.map_data([right, c.y])[0]
            left_y = f(left_x)
            right_y = f(right_x)

            left_pt, right_pt = c.map_screen([[left_x, left_y], [right_x, right_y]])

            with gc:
                gc.set_line_dash(self.line_style_)
                gc.set_stroke_color(self.line_color_)
                gc.set_line_width(self.line_width)
                gc.move_to(*left_pt)
                gc.line_to(*right_pt)
                gc.stroke_path()

        return
Exemplo n.º 5
0
def _create_plot_component():
    # Create some data
    npts = 200
    x = sort(random(npts))
    y = random(npts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="red",
              marker_size=4,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot With Rectangular Selection"
    plot.line_width = 1
    plot.padding = 50

    # Right now, some of the tools are a little invasive, and we need the
    # actual ScatterPlot object to give to them
    my_plot = plot.plots["my_plot"][0]

    # Attach some tools to the plot
    rect_selection = RectangularSelection(
        component=my_plot,
        selection_datasource=my_plot.index,
        drag_button="left",
        metadata_name='selections',
    )
    my_plot.tools.append(rect_selection)
    my_plot.tools.append(ScatterInspector(my_plot, selection_mode='toggle'))
    my_plot.active_tool = rect_selection

    lasso_overlay = LassoOverlay(lasso_selection=rect_selection,
                                 component=my_plot)
    my_plot.overlays.append(lasso_overlay)

    scatter_overlay = ScatterInspectorOverlay(
        component=my_plot,
        selection_color='cornflowerblue',
        selection_marker_size=int(my_plot.marker_size) + 3,
        selection_marker='circle')
    my_plot.overlays.append(scatter_overlay)

    return plot
Exemplo n.º 6
0
    def overlay_selection(self, plot):
        lasso_selection = LassoSelection(component=plot,
                                         selection_datasource=plot.index,
                                         drag_button="left")
        plot.active_tool = lasso_selection
        plot.tools.append(ScatterInspector(plot))
        lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                     component=plot)
        plot.overlays.append(lasso_overlay)

        # Uncomment this if you would like to see incremental updates:
        # lasso_selection.incremental_select = True
        self.index_datasource = plot.index
 def install(self):
     scatter = self.widget.scatter
     self._tool = tool = LassoSelection(scatter,
         drag_button = 'left',
         selection_datasource = scatter.index,
         metadata_name = 'lasso_selection',
     )
     tool.on_trait_change(self._selection_changed, 'selection_changed')
     
     self._overlay = LassoOverlay(scatter,
         lasso_selection = self._tool,
     )
     scatter.tools.append(self._tool)
     scatter.overlays.append(self._overlay)
Exemplo n.º 8
0
def _create_plot_component():

    # Create some data
    npts = 2000
    x = sort(random(npts))
    y = random(npts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              name="my_plot",
              marker="circle",
              index_sort="ascending",
              color="red",
              marker_size=4,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot With Lasso Selection"
    plot.line_width = 1
    plot.padding = 50

    # Right now, some of the tools are a little invasive, and we need the
    # actual ScatterPlot object to give to them
    my_plot = plot.plots["my_plot"][0]

    # Attach some tools to the plot
    lasso_selection = LassoSelection(component=my_plot,
                                     selection_datasource=my_plot.index,
                                     drag_button="left")
    #drag_button="right")
    my_plot.active_tool = lasso_selection
    my_plot.tools.append(ScatterInspector(my_plot))
    lasso_overlay = LassoOverlay(lasso_selection=lasso_selection,
                                 component=my_plot)
    my_plot.overlays.append(lasso_overlay)

    # Uncomment this if you would like to see incremental updates:
    #lasso_selection.incremental_select = True

    return plot
Exemplo n.º 9
0
    def _plot_default(self):
        stations = self.stations
        lon, lat = self._proj(stations['LON'].view(numpy.ndarray),
                              stations['LAT'].view(numpy.ndarray))

        shift = self._shift
        lon = (lon + shift / 2.) / (shift)
        lat = (lat + shift / 2.) / (shift)

        plot = Plot(ArrayPlotData(index=lon, value=lat))
        plot.plot(
            ("index", "value"),
            type="scatter",
            name="stations",
            marker="dot",
            outline_color='black',
            color=(1., 0., 0., 0.2),
            line_width=1.,
            marker_size=1,
        )

        mbtiles_fname = os.path.join('Data', 'tiles.mbtiles')
        if os.path.exists(mbtiles_fname):
            tile_cache = MBTileManager(filename=mbtiles_fname,
                                       min_level=0,
                                       max_level=7)
        else:
            tile_cache = HTTPTileManager(
                min_level=0,
                max_level=6,
                server='oatile1.mqcdn.com',
                url='/tiles/1.0.0/sat/%(zoom)d/%(row)d/%(col)d.jpg',
            )

        # Right now, some of the tools are a little invasive, and we need the
        # actual ScatterPlot object to give to them
        scatter = plot.plots['stations'][0]

        map = Map(scatter, tile_cache=tile_cache, alpha=0.8, zoom_level=2)
        scatter.underlays.append(map)

        map.on_trait_change(lambda new: self._update_scatter(scatter, new),
                            'zoom_level')
        self._update_scatter(scatter, map.zoom_level)

        lasso_selection = LassoSelection(component=scatter,
                                         selection_datasource=scatter.index)

        scatter.tools.append(lasso_selection)
        scatter.overlays.append(
            LassoOverlay(component=scatter,
                         selection_fill_color='lawngreen',
                         selection_border_color='lightgreen',
                         selection_alpha=0.5,
                         selection_border_width=2.0,
                         lasso_selection=lasso_selection))

        scatter.overlays.append(
            ScatterInspectorOverlay(scatter,
                                    selection_metadata_name='selection',
                                    selection_marker_size=4,
                                    selection_color="lawngreen"))
        scatter.index.on_trait_change(self._metadata_handler,
                                      "metadata_changed")

        scatter.tools.append(PanTool(scatter, drag_button='right'))
        scatter.tools.append(ZoomTool(scatter))

        plot.index_axis.title = "Longitude"
        plot.index_axis.tick_label_formatter = self._convert_lon
        plot.value_axis.title = "Latitude"
        plot.value_axis.tick_label_formatter = self._convert_lat

        plot.padding_right = plot.padding_top = 2

        container = OverlayPlotContainer(use_backbuffer=True,
                                         bgcolor="sys_window")
        container.add(plot)
        container.bgcolor = "sys_window"

        return container