Esempio n. 1
0
def get_contour_line_plot():
    NPOINTS_X, NPOINTS_Y = 600, 300

    # Create a scalar field to contour
    xs = np.linspace(-2 * np.pi, +2 * np.pi, NPOINTS_X)
    ys = np.linspace(-1.5 * np.pi, +1.5 * np.pi, NPOINTS_Y)
    x, y = np.meshgrid(xs, ys)
    z = scipy.special.jn(2, x) * y * x

    index = GridDataSource(xdata=xs, ydata=ys)
    index_mapper = GridMapper(range=DataRange2D(index))

    value = ImageData(data=z, value_depth=1)
    color_mapper = dc.Blues(DataRange1D(value))

    contour_plot = ContourLinePlot(index=index,
                                   index_mapper=index_mapper,
                                   value=value,
                                   colors=color_mapper,
                                   widths=list(range(1, 11)),
                                   **PLOT_DEFAULTS)

    add_axes(contour_plot, x_label='x', y_label='y')

    return contour_plot
Esempio n. 2
0
    def _theta_plot_default(self):

        theta = self.theta
        nclasses = theta.shape[0]

        # create a plot data object and give it this data
        plot_data = ArrayPlotData()

        plot_data.set_data('classes', list(range(nclasses)))

        # create the plot
        plot = Plot(plot_data)

        # --- plot theta samples
        if self.theta_samples is not None:
            self._plot_samples(plot, plot_data)

        # --- plot values of theta
        plots = self._plot_theta_values(plot, plot_data)

        # --- adjust plot appearance

        plot.aspect_ratio = 1.6 if is_display_small() else 1.7

        # adjust axis bounds
        y_high = theta.max()
        if self.theta_samples is not None:
            y_high = max(y_high, self.theta_samples.max())

        plot.range2d = DataRange2D(low=(-0.2, 0.0),
                                   high=(nclasses - 1 + 0.2, y_high * 1.1))

        # create new horizontal axis
        label_axis = self._create_increment_one_axis(plot, 0., nclasses,
                                                     'bottom')
        label_axis.title = 'True classes'
        self._add_index_axis(plot, label_axis)

        # label vertical axis
        plot.value_axis.title = 'Probability'

        # add legend
        legend = Legend(component=plot,
                        plots=plots,
                        align="ur",
                        border_padding=10)
        legend.tools.append(LegendTool(legend, drag_button="left"))
        legend.padding_right = -100
        plot.overlays.append(legend)

        container = VPlotContainer(width=plot.width + 100, halign='left')
        plot.padding_bottom = 50
        plot.padding_top = 10
        plot.padding_left = 0
        container.add(plot)
        container.bgcolor = 0xFFFFFF

        self.decorate_plot(container, theta)

        return container
Esempio n. 3
0
 def _compute_range2d(self):
     low = min(0.6, self.model.theta.min()-0.05)
     if self.theta_samples_valid:
         low = min(low, self.theta_samples.min()-0.05)
     range2d = DataRange2D(low=(0., low),
                           high=(self.model.theta.shape[0]+1, 1.))
     return range2d
def get_contour_poly_plot():
    NPOINTS_X, NPOINTS_Y = 600, 300

    # Create a scalar field to contour
    xs = np.linspace(-2 * np.pi, +2 * np.pi, NPOINTS_X)
    ys = np.linspace(-1.5 * np.pi, +1.5 * np.pi, NPOINTS_Y)
    x, y = np.meshgrid(xs, ys)
    z = scipy.special.jn(2, x) * y * x

    # FIXME: we have set the xbounds and ybounds manually to work around
    # a bug in CountourLinePlot, see comment in contour_line_plot.py at
    # line 112 (the workaround is the +1 at the end)
    xs_bounds = np.linspace(xs[0], xs[-1], z.shape[1] + 1)
    ys_bounds = np.linspace(ys[0], ys[-1], z.shape[0] + 1)
    index = GridDataSource(xdata=xs_bounds, ydata=ys_bounds)
    index_mapper = GridMapper(range=DataRange2D(index))

    value = ImageData(data=z, value_depth=1)
    color_mapper = dc.Blues(DataRange1D(value))

    contour_plot = ContourPolyPlot(index=index,
                                   index_mapper=index_mapper,
                                   value=value,
                                   colors=color_mapper,
                                   **PLOT_DEFAULTS)

    add_axes(contour_plot, x_label='x', y_label='y')

    return contour_plot
Esempio n. 5
0
def get_cmap_image_plot():
    # Create a scalar field to colormap
    NPOINTS = 200

    xs = np.linspace(-2 * np.pi, +2 * np.pi, NPOINTS)
    ys = np.linspace(-1.5*np.pi, +1.5*np.pi, NPOINTS)
    x, y = np.meshgrid(xs, ys)
    z = scipy.special.jn(2, x)*y*x

    index = GridDataSource(xdata=xs, ydata=ys)
    index_mapper = GridMapper(range=DataRange2D(index))

    color_source = ImageData(data=z, value_depth=1)
    color_mapper = dc.Spectral(DataRange1D(color_source))

    cmap_plot = CMapImagePlot(
        index=index,
        index_mapper=index_mapper,
        value=color_source,
        value_mapper=color_mapper,
        **PLOT_DEFAULTS
    )

    add_axes(cmap_plot, x_label='x', y_label='y')

    return cmap_plot
Esempio n. 6
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)
Esempio n. 7
0
    def _compute_range2d(self):
        #if len(self.df)> 100:
        lowy = min(self.df.low[-100:])
        lowy = lowy * 0.998
        lowx = len(self.df) - 100

        highy = max(self.df.high[-300:])
        highy = highy * 1.002

        highx = len(self.df)
        range2d = DataRange2D(low=(lowx, lowy),
                              high=(highx, highy))
        return range2d
Esempio n. 8
0
def get_image_from_file():
    import os.path
    filename = os.path.join('..', '..', 'demo', 'basic', 'capitol.jpg')
    image_source = ImageData.fromfile(filename)

    w, h = image_source.get_width(), image_source.get_height()
    index = GridDataSource(np.arange(w), np.arange(h))
    index_mapper = GridMapper(
        range=DataRange2D(low=(0, 0), high=(w - 1, h - 1)))

    image_plot = ImagePlot(index=index,
                           value=image_source,
                           index_mapper=index_mapper,
                           origin='top left',
                           **PLOT_DEFAULTS)

    add_axes(image_plot, x_label='x', y_label='y')

    return image_plot
Esempio n. 9
0
def get_image_plot():
    # Create some RGBA image data
    image = np.zeros((200, 400, 4), dtype=np.uint8)
    image[:, 0:40, 0] += 255  # Vertical red stripe
    image[0:25, :, 1] += 255  # Horizontal green stripe; also yellow square
    image[-80:, -160:, 2] += 255  # Blue square
    image[:, :, 3] = 255

    index = GridDataSource(np.linspace(0, 4., 400), np.linspace(-1, 1., 200))
    index_mapper = GridMapper(range=DataRange2D(low=(0, -1), high=(4., 1.)))

    image_source = ImageData(data=image, value_depth=4)

    image_plot = ImagePlot(index=index,
                           value=image_source,
                           index_mapper=index_mapper,
                           **PLOT_DEFAULTS)

    add_axes(image_plot, x_label='x', y_label='y')

    return image_plot
Esempio n. 10
0
    def _plot_default(self):
        distr_len = len(self.data)

        # PolygonPlot holding the circles of the Hinton diagram
        polyplot = Plot(self.plot_data)
        for idx in range(distr_len):
            p = polyplot.plot(('x%d' % idx, 'y%d' % idx),
                              type="polygon",
                              face_color=get_class_color(idx),
                              edge_color='black')

        self._set_title(polyplot)
        self._remove_grid_and_axes(polyplot)

        # create x axis for labels
        axis = self._create_increment_one_axis(polyplot, 1., distr_len,
                                               'bottom')
        self._add_index_axis(polyplot, axis)

        # create y axis for probability density
        #prob_axis = self._create_probability_axis(polyplot)
        #polyplot.value_axis = prob_axis
        #polyplot.underlays.append(prob_axis)

        # tweak some of the plot properties
        range2d = DataRange2D(low=(0.5, 0.), high=(distr_len + 0.5, 1.))
        polyplot.range2d = range2d
        polyplot.aspect_ratio = ((range2d.x_range.high - range2d.x_range.low) /
                                 (range2d.y_range.high - range2d.y_range.low))

        polyplot.border_visible = False
        polyplot.padding = [0, 0, 25, 25]

        # create a container to position the plot and the colorbar side-by-side
        container = HPlotContainer(use_backbuffer=True, valign='center')
        container.add(polyplot)
        container.bgcolor = 0xFFFFFF  # light gray: 0xEEEEEE

        self.decorate_plot(container, self.data)
        return container
Esempio n. 11
0
    def _theta_plot_default(self):
        theta = self.theta
        nannotators = theta.shape[0]
        samples = self.theta_samples

        # plot data object
        plot_data = ArrayPlotData()

        # create the plot
        plot = Plot(plot_data)

        # --- plot theta as vertical dashed lines
        # add vertical lines extremes
        plot_data.set_data('line_extr', [0., 1.])

        for k in range(nannotators):
            name = self._theta_name(k)
            plot_data.set_data(name, [theta[k], theta[k]])

        plots = {}
        for k in range(nannotators):
            name = self._theta_name(k)
            line_plot = plot.plot(
                (name, 'line_extr'),
                line_width = 2.,
                color = get_annotator_color(k),
                line_style = 'dash',
                name = name
            )
            plots[name] = line_plot

        # --- plot samples as distributions
        if samples is not None:
            bins = np.linspace(0., 1., 100)
            max_hist = 0.
            for k in range(nannotators):
                name = self._theta_name(k) + '_distr_'
                hist, x = np.histogram(samples[:,k], bins=bins)
                hist = hist / float(hist.sum())
                max_hist = max(max_hist, hist.max())

                # make "bars" out of histogram values
                y = np.concatenate(([0], np.repeat(hist, 2), [0]))
                plot_data.set_data(name+'x', np.repeat(x, 2))
                plot_data.set_data(name+'y', y)

            for k in range(nannotators):
                name = self._theta_name(k) + '_distr_'
                plot.plot((name+'x', name+'y'),
                          line_width = 2.,
                          color = get_annotator_color(k)
                          )

        # --- adjust plot appearance

        plot.aspect_ratio = 1.6 if is_display_small() else 1.7
        plot.padding = [20,0,10,40]

        # adjust axis bounds
        x_low, x_high = theta.min(), theta.max()
        y_low, y_high = 0., 1.
        if samples is not None:
            x_high = max(x_high, samples.max())
            x_low = min(x_low, samples.min())
            y_high = max_hist

        plot.range2d = DataRange2D(
            low  = (max(x_low-0.05, 0.), y_low),
            high = (min(x_high*1.1, 1.), min(y_high*1.1, 1.))
        )

        # label axes
        plot.value_axis.title = 'Probability'
        plot.index_axis.title = 'Theta'

        # add legend
        legend = Legend(component=plot, plots=plots,
                        align="ul", padding=5)
        legend.tools.append(LegendTool(legend, drag_button="left"))
        plot.overlays.append(legend)

        container = VPlotContainer()
        container.add(plot)
        container.bgcolor = 0xFFFFFF

        self.decorate_plot(container, theta)
        self._set_title(plot)

        return container