Пример #1
0
def add_default_grids(plot, orientation="normal", tick_gen=None):
    """
    Creates horizontal and vertical gridlines for a plot.  Assumes that the
    index is horizontal and value is vertical by default; set orientation to
    something other than "normal" if they are flipped.
    """
    if orientation in ("normal", "h"):
        v_mapper = plot.index_mapper
        h_mapper = plot.value_mapper
    else:
        v_mapper = plot.value_mapper
        h_mapper = plot.index_mapper

    vgrid = PlotGrid(
        mapper=v_mapper,
        orientation='vertical',
        component=plot,
        line_color="lightgray",
        line_style="dot",
        tick_generator=tick_gen)

    hgrid = PlotGrid(
        mapper=h_mapper,
        orientation='horizontal',
        component=plot,
        line_color="lightgray",
        line_style="dot",
        tick_generator=ScalesTickGenerator())

    plot.underlays.append(vgrid)
    plot.underlays.append(hgrid)
    return hgrid, vgrid
Пример #2
0
def _create_plot_component():

    # Generate some data for the eye diagram.
    num_samples = 5000
    samples_per_symbol = 24
    y = demo_data(num_samples, samples_per_symbol)

    # Compute the eye diagram array.
    ybounds = (-0.25, 1.25)
    grid = grid_count(y,
                      2 * samples_per_symbol,
                      offset=16,
                      size=(480, 480),
                      bounds=ybounds).T

    # Convert the array to floating point, and replace 0 with np.nan.
    # These points will be transparent in the image plot.
    grid = grid.astype(np.float32)
    grid[grid == 0] = np.nan

    #---------------------------------------------------------------------
    # The rest of the function creates the chaco image plot.

    pd = ArrayPlotData()
    pd.set_data("eyediagram", grid)

    plot = Plot(pd)
    img_plot = plot.img_plot("eyediagram",
                             xbounds=(0, 2),
                             ybounds=ybounds,
                             bgcolor=(0, 0, 0),
                             colormap=cool)[0]

    # Tweak some of the plot properties
    plot.title = "Eye Diagram"
    plot.padding = 50

    # Axis grids
    vgrid = PlotGrid(component=plot,
                     mapper=plot.index_mapper,
                     orientation='vertical',
                     line_color='gray',
                     line_style='dot')
    hgrid = PlotGrid(component=plot,
                     mapper=plot.value_mapper,
                     orientation='horizontal',
                     line_color='gray',
                     line_style='dot')
    plot.underlays.append(vgrid)
    plot.underlays.append(hgrid)

    # Add pan and zoom tools.
    plot.tools.append(PanTool(plot))
    zoom = ZoomTool(component=img_plot, tool_mode="box", always_on=False)
    img_plot.overlays.append(zoom)

    return plot
Пример #3
0
    def _init_components(self):
        # Since this is called after the HasTraits constructor, we have to make
        # sure that we don't blow away any components that the caller may have
        # already set.

        if self.range2d is None:
            self.range2d = DataRange2D()

        if self.index_mapper is None:
            if self.index_scale == "linear":
                imap = LinearMapper(range=self.range2d.x_range)
            else:
                imap = LogMapper(range=self.range2d.x_range)
            self.index_mapper = imap

        if self.value_mapper is None:
            if self.value_scale == "linear":
                vmap = LinearMapper(range=self.range2d.y_range)
            else:
                vmap = LogMapper(range=self.range2d.y_range)
            self.value_mapper = vmap

        if self.x_ticks is None:
            self.x_ticks = ScalesTickGenerator(
                scale=self._make_scale(self.index_scale))
        if self.y_ticks is None:
            self.y_ticks = ScalesTickGenerator(
                scale=self._make_scale(self.value_scale))

        if self.x_grid is None:
            self.x_grid = PlotGrid(mapper=self.x_mapper,
                                   orientation="vertical",
                                   line_color="lightgray",
                                   line_style="dot",
                                   component=self,
                                   tick_generator=self.x_ticks)
        if self.y_grid is None:
            self.y_grid = PlotGrid(mapper=self.y_mapper,
                                   orientation="horizontal",
                                   line_color="lightgray",
                                   line_style="dot",
                                   component=self,
                                   tick_generator=self.y_ticks)
        if self.x_axis is None:
            self.x_axis = PlotAxis(mapper=self.x_mapper,
                                   orientation="bottom",
                                   component=self,
                                   tick_generator=self.x_ticks)
        if self.y_axis is None:
            self.y_axis = PlotAxis(mapper=self.y_mapper,
                                   orientation="left",
                                   component=self,
                                   tick_generator=self.y_ticks)
def _create_plot_component():

    # Create some x-y data series to plot
    plot_area = OverlayPlotContainer(border_visible=True)
    container = HPlotContainer(padding=50, bgcolor="transparent")
    #container.spacing = 15

    x = linspace(-2.0, 10.0, 100)
    for i in range(5):
        color = tuple(COLOR_PALETTE[i])
        y = jn(i, x)
        renderer = create_line_plot((x, y), color=color)
        plot_area.add(renderer)
        #plot_area.padding_left = 20

        axis = PlotAxis(
            orientation="left",
            resizable="v",
            mapper=renderer.y_mapper,
            axis_line_color=color,
            tick_color=color,
            tick_label_color=color,
            title_color=color,
            bgcolor="transparent",
            title="jn_%d" % i,
            border_visible=True,
        )
        axis.bounds = [60, 0]
        axis.padding_left = 10
        axis.padding_right = 10

        container.add(axis)

        if i == 4:
            # Use the last plot's X mapper to create an X axis and a
            # vertical grid
            x_axis = PlotAxis(orientation="bottom",
                              component=renderer,
                              mapper=renderer.x_mapper)
            renderer.overlays.append(x_axis)
            grid = PlotGrid(mapper=renderer.x_mapper,
                            orientation="vertical",
                            line_color="lightgray",
                            line_style="dot")
            renderer.underlays.append(grid)

    # Add the plot_area to the horizontal container
    container.add(plot_area)

    # Attach some tools to the plot
    broadcaster = BroadcasterTool()
    for plot in plot_area.components:
        broadcaster.tools.append(PanTool(plot))

    # Attach the broadcaster to one of the plots.  The choice of which
    # plot doesn't really matter, as long as one of them has a reference
    # to the tool and will hand events to it.
    plot.tools.append(broadcaster)

    return container
Пример #5
0
    def configure_plot(plot, xlabel='Time (s)'):
        """ Set up colors, grids, etc. on plot objects.
        """
        plot.bgcolor = 'white'
        plot.border_visible = True
        plot.padding = [40, 15, 15, 20]
        plot.color = 'darkred'
        plot.line_width = 1.1

        vertical_grid = PlotGrid(component=plot,
                                 mapper=plot.index_mapper,
                                 orientation='vertical',
                                 line_color="gray",
                                 line_style='dot',
                                 use_draw_order=True)

        horizontal_grid = PlotGrid(component=plot,
                                   mapper=plot.value_mapper,
                                   orientation='horizontal',
                                   line_color="gray",
                                   line_style='dot',
                                   use_draw_order=True)

        vertical_axis = PlotAxis(orientation='left',
                                 mapper=plot.value_mapper,
                                 use_draw_order=True)

        horizontal_axis = PlotAxis(orientation='bottom',
                                   title=xlabel,
                                   mapper=plot.index_mapper,
                                   use_draw_order=True)

        plot.underlays.append(vertical_grid)
        plot.underlays.append(horizontal_grid)

        # Have to add axes to overlays because we are backbuffering the main plot,
        # and only overlays get to render in addition to the backbuffer.
        plot.overlays.append(vertical_axis)
        plot.overlays.append(horizontal_axis)
Пример #6
0
 def _add_zero_axis(self):
     xgrid = PlotGrid(
         mapper=self.x_mapper,
         orientation='vertical',
         line_weight=1,
         grid_interval=1,
         component=self,
         data_min=-0.5,
         data_max=0.5,
         # transverse_bounds=(-99, 99),
         transverse_mapper=self.y_mapper)
     self.underlays.append(xgrid)
     ygrid = PlotGrid(
         mapper=self.y_mapper,
         orientation='horizontal',
         line_weight=1,
         grid_interval=1,
         component=self,
         data_min=-0.5,
         data_max=0.5,
         # transverse_bounds=(-99, 99),
         transverse_mapper=self.x_mapper)
     self.underlays.append(ygrid)
Пример #7
0
def create_gridded_scatter_plot(x,
                                y,
                                orientation="h",
                                color="red",
                                width=1.0,
                                fill_color="red",
                                marker="square",
                                marker_size=2,
                                value_mapper_class=LinearMapper,
                                padding=30):

    assert len(x) == len(y)

    # If you know it is monotonically increasing, sort_order can
    # be set to 'ascending'
    index = ArrayDataSource(x, sort_order='none')
    value = ArrayDataSource(y, sort_order="none")

    index_range = DataRange1D(tight_bounds=False)
    index_range.add(index)
    index_mapper = LinearMapper(range=index_range)

    value_range = DataRange1D(tight_bounds=False)
    value_range.add(value)
    value_mapper = value_mapper_class(range=value_range)

    plot = ScatterPlot(
        index=index,
        value=value,
        index_mapper=index_mapper,
        value_mapper=value_mapper,
        orientation=orientation,
        color=color,
        fill_color=fill_color,
        marker=marker,
        marker_size=marker_size,
        padding=[40, 15, 15, 20],  # left, right, top, bottom
        border_visible=True,
        border_width=1,
        bgcolor="white",
        use_backbuffer=True,
        backbuffer_padding=False,
        unified_draw=True,
        draw_layer="plot",
        overlay_border=True)

    vertical_grid = PlotGrid(component=plot,
                             mapper=index_mapper,
                             orientation='vertical',
                             line_color="gray",
                             line_style='dot',
                             use_draw_order=True)

    horizontal_grid = PlotGrid(component=plot,
                               mapper=value_mapper,
                               orientation='horizontal',
                               line_color="gray",
                               line_style='dot',
                               use_draw_order=True)

    vertical_axis = PlotAxis(orientation='left',
                             mapper=plot.value_mapper,
                             use_draw_order=True)

    horizontal_axis = PlotAxis(orientation='bottom',
                               title='Time (s)',
                               mapper=plot.index_mapper,
                               use_draw_order=True)

    plot.underlays.append(vertical_grid)
    plot.underlays.append(horizontal_grid)

    # Have to add axes to overlays because we are backbuffering the main plot,
    # and only overlays get to render in addition to the backbuffer.
    plot.overlays.append(vertical_axis)
    plot.overlays.append(horizontal_axis)
    return plot
Пример #8
0
    def new_series(self, x=None, y=None, plotid=0, **kw):
        '''
        '''

        plot, scatter, _line = super(ResidualsGraph,
                                     self).new_series(x=x,
                                                      y=y,
                                                      plotid=plotid,
                                                      **kw)
        for underlay in plot.underlays:
            if underlay.orientation == 'bottom':
                underlay.visible = False
                underlay.padding_bottom = 0
        plot.padding_bottom = 0

        x, y, res = self.calc_residuals(plotid=plotid)

        ressplit = self._split_residual(x, res)
        resneg = ArrayDataSource(ressplit[1])
        xneg = ArrayDataSource(ressplit[0])
        respos = ArrayDataSource(ressplit[3])
        xpos = ArrayDataSource(ressplit[2])

        yrange = DataRange1D(ArrayDataSource(res))

        ymapper = LinearMapper(range=yrange)

        container = self._container_factory(type='o',
                                            padding=[50, 15, 0, 30],
                                            height=75,
                                            resizable='h')
        bar = BarPlot(
            index=xneg,
            value=resneg,
            index_mapper=scatter.index_mapper,
            value_mapper=ymapper,
            bar_width=0.2,
            line_color='blue',
            fill_color='blue',
            border_visible=True,
        )

        #        left_axis = PlotAxis(bar, orientation = 'left')
        # bottom_axis=PlotAxis(bar,orientaiton='bottom')

        kw = dict(vtitle='residuals')
        if self.xtitle:
            kw['htitle'] = self.xtitle
        add_default_axes(bar, **kw)
        hgrid = PlotGrid(mapper=ymapper,
                         component=bar,
                         orientation='horizontal',
                         line_color='lightgray',
                         line_style='dot')

        bar.underlays.append(hgrid)
        #        bar.underlays.append(left_axis)
        #        bar.underlays.append(bottom_axis)

        bar2 = BarPlot(
            index=xpos,
            value=respos,
            index_mapper=scatter.index_mapper,
            value_mapper=ymapper,
            bar_width=0.2,
            line_color='green',
            fill_color='green',
            # bgcolor = 'green',
            resizable='hv',
            border_visible=True,
            # padding = [30, 5, 0, 30]
        )
        bar2.overlays.append(GuideOverlay(bar2, value=0, color=(0, 0, 0)))
        bar2.underlays.append(hgrid)
        container.add(bar)
        container.add(bar2)

        # container.add(PlotLabel('foo'))

        self.residual_plots = [bar, bar2]
        self.plotcontainer.add(container)
Пример #9
0
def _create_plot_components():
    # Create the data and datasource objects
    # In order for the date axis to work, the index data points need to
    # be in units of seconds since the epoch.  This is because we are using
    # the CalendarScaleSystem, whose formatters interpret the numerical values
    # as seconds since the epoch.
    high = 1.
    numpoints = 5000

    random.seed(1000)

    index = create_dates(numpoints)
    price = 100 * cumprod(random.lognormal(0.0, 0.04, size=numpoints))
    changes = price / price[0] - 1.

    index_ds = ArrayDataSource(index)
    value_ds = ArrayDataSource(changes, sort_order="none")
    value_range = DataRange1D(value_ds, low=-high, high=high)

    index_mapper = LinearMapper(range=DataRange1D(index_ds),
                                stretch_data=False)

    horizon = HorizonPlot(
        bands=4,
        index=index_ds,
        value=value_ds,
        index_mapper=index_mapper,
        value_mapper=BandedMapper(range=DataRange1D(low=0, high=high)),
        color_mapper=cmap(range=value_range),
    )
    horizon.tools.append(
        PanTool(horizon, constrain=True, constrain_direction="x"))
    axis = PlotAxis(mapper=horizon.value_mapper,
                    component=horizon,
                    orientation="left",
                    tick_label_position="outside")
    horizon.overlays.append(axis)

    bottom_axis = PlotAxis(
        horizon,
        orientation="bottom",
        tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem()))
    horizon.overlays.append(bottom_axis)

    filled = FilledLinePlot(
        index=index_ds,
        value=value_ds,
        index_mapper=index_mapper,
        value_mapper=LinearMapper(range=value_range, stretch_data=False),
        fill_color=(0.81960784, 0.89803922, 0.94117647),
        edge_color='transparent',
    )
    filled.tools.append(
        PanTool(filled, constrain=True, constrain_direction="x"))
    axis = PlotAxis(mapper=filled.value_mapper,
                    component=filled,
                    orientation="left",
                    tick_label_position="outside")
    filled.overlays.append(axis)

    grid = PlotGrid(
        mapper=filled.value_mapper,
        component=filled,
        orientation='horizontal',
        line_color='lightgray',
        line_style="dot",
    )
    filled.underlays.append(grid)

    colormap = horizon.color_mapper
    colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
                        color_mapper=colormap,
                        orientation='v',
                        resizable='v',
                        width=20,
                        padding=20)

    padding = (40, 20, 0, 0)
    over1 = HPlotContainer(use_backbuffer=True,
                           padding=padding,
                           padding_top=20)
    over1.add(filled)
    over1.add(colorbar)

    over2 = OverlayPlotContainer(padding=padding, padding_bottom=40)
    over2.add(horizon)

    return over1, over2