Example #1
0
    def kde(self, x, y, data=None):
        data = self.data if data is None else data
        plot_opts = dict(self._plot_opts)
        invert = self.kwds.get('orientation', False) == 'horizontal'
        opts = dict(plot=dict(plot_opts, invert_axes=invert),
                    style=self._style_opts,
                    norm=self._norm_opts)
        opts = {
            'Distribution': opts,
            'Area': opts,
            'NdOverlay': {
                'plot': dict(plot_opts, legend_limit=0)
            }
        }

        if y and self.by:
            ds = Dataset(data)
            return ds.to(Distribution, y, [], self.by).overlay().opts(opts)
        elif y:
            return Distribution(data, y, []).opts(opts)

        if self.columns:
            data = data[self.columns]
        df = pd.melt(data,
                     var_name=self.group_label,
                     value_name=self.value_label)
        ds = Dataset(df)
        if len(df):
            overlay = ds.to(Distribution, self.value_label).overlay()
        else:
            vdim = self.value_label + ' Density'
            overlay = NdOverlay({0: Area([], self.value_label, vdim)},
                                [self.group_label])
        return overlay.relabel(**self._relabel).opts(opts)
Example #2
0
 def test_curve_style_mapping_constant_value_dimensions(self):
     vdims = ['y', 'num', 'cat']
     ndoverlay = NdOverlay({
         0:
         Curve([(0, 1, 0, 'A'), (1, 0, 0, 'A')], vdims=vdims),
         1:
         Curve([(0, 1, 0, 'B'), (1, 1, 0, 'B')], vdims=vdims),
         2:
         Curve([(0, 1, 1, 'A'), (1, 2, 1, 'A')], vdims=vdims),
         3:
         Curve([(0, 1, 1, 'B'), (1, 3, 1, 'B')], vdims=vdims)
     }).opts({
         'Curve':
         dict(color=dim('num').categorize({
             0: 'red',
             1: 'blue'
         }),
              linestyle=dim('cat').categorize({
                  'A': '-.',
                  'B': '-'
              }))
     })
     plot = mpl_renderer.get_plot(ndoverlay)
     for k, sp in plot.subplots.items():
         artist = sp.handles['artist']
         color = artist.get_color()
         if ndoverlay[k].iloc[0, 2] == 0:
             self.assertEqual(color, 'red')
         else:
             self.assertEqual(color, 'blue')
         linestyle = artist.get_linestyle()
         if ndoverlay[k].iloc[0, 3] == 'A':
             self.assertEqual(linestyle, '-.')
         else:
             self.assertEqual(linestyle, '-')
Example #3
0
 def test_curve_style_mapping_ndoverlay_dimensions(self):
     ndoverlay = NdOverlay(
         {
             (0, 'A'): Curve([1, 2, 0]),
             (0, 'B'): Curve([1, 2, 1]),
             (1, 'A'): Curve([1, 2, 2]),
             (1, 'B'): Curve([1, 2, 3])
         }, ['num', 'cat']).opts({
             'Curve':
             dict(color=dim('num').categorize({
                 0: 'red',
                 1: 'blue'
             }),
                  linestyle=dim('cat').categorize({
                      'A': '-.',
                      'B': '-'
                  }))
         })
     plot = mpl_renderer.get_plot(ndoverlay)
     for (num, cat), sp in plot.subplots.items():
         artist = sp.handles['artist']
         color = artist.get_color()
         if num == 0:
             self.assertEqual(color, 'red')
         else:
             self.assertEqual(color, 'blue')
         linestyle = artist.get_linestyle()
         if cat == 'A':
             self.assertEqual(linestyle, '-.')
         else:
             self.assertEqual(linestyle, '-')
Example #4
0
    def hist(self, x, y, data=None):
        plot_opts = dict(self._plot_opts)
        invert = self.kwds.get('orientation', False) == 'horizontal'
        opts = dict(plot=dict(plot_opts, labelled=['x'], invert_axes=invert),
                    style=self._style_opts,
                    norm=self._norm_opts)
        hist_opts = {
            'num_bins': self.kwds.get('bins', 10),
            'bin_range': self.kwds.get('bin_range', None),
            'normed': self.kwds.get('normed', False)
        }

        data = self.data if data is None else data
        ds = Dataset(data)
        if y and self.by:
            return histogram(ds.to(Dataset, [], y, self.by), **hist_opts).\
                overlay().opts({'Histogram': opts})
        elif y:
            return histogram(ds, dimension=y, **hist_opts).\
                opts({'Histogram': opts})

        hists = {}
        columns = self.columns or data.columns
        for col in columns:
            hist = histogram(ds, dimension=col, **hist_opts)
            ranges = {hist.vdims[0].name: self._dim_ranges['y']}
            hists[col] = (hist.redim.range(**ranges).relabel(
                **self._relabel).opts(**opts))
        return NdOverlay(hists)
Example #5
0
    def chart(self, element, x, y, data=None):
        "Helper method for simple x vs. y charts"
        if x and y:
            return self.single_chart(element, x, y, data)

        # Note: Loading dask dataframe into memory due to rename bug
        data = (self.data if data is None else data)
        if self.use_dask: data = data.compute()
        opts = dict(plot=dict(self._plot_opts, labelled=['x']),
                    norm=self._norm_opts,
                    style=self._style_opts)

        if self.use_index or x:
            if self.use_index is not None and isinstance(self.use_index, bool):
                x = x or data.index.name or 'index'
            else:
                x = self.use_index
            columns = self.columns or data.columns
            charts = {}
            for c in columns:
                chart = element(data, x, c).redim(**{c: self.value_label})
                ranges = {x: self._dim_ranges['x'], c: self._dim_ranges['y']}
                charts[c] = (chart.relabel(**self._relabel).redim.range(
                    **ranges).opts(**opts))
            return NdOverlay(charts)
        else:
            raise ValueError('Could not determine what to plot. Expected '
                             'either x and y parameters to be declared '
                             'or use_index to be enabled.')
Example #6
0
    def kde(self, x, y, data=None):
        data, x, y = self._process_args(data, x, y)
        opts = dict(plot=self._plot_opts,
                    style=self._style_opts,
                    norm=self._norm_opts)
        opts = {
            'Distribution': opts,
            'Area': opts,
            'NdOverlay': {
                'plot': dict(self._plot_opts, legend_limit=0)
            }
        }

        if not isinstance(y, (list, tuple)):
            ranges = {y: self._dim_ranges['x']}
            if self.by:
                dists = Dataset(data).to(Distribution, y, [], self.by)
                dists = dists.layout() if self.subplots else dists.overlay()
            else:
                dists = Distribution(data, y, [])
        else:
            ranges = {self.value_label: self._dim_ranges['x']}
            data = data[y]
            df = pd.melt(data,
                         var_name=self.group_label,
                         value_name=self.value_label)
            ds = Dataset(df)
            if len(df):
                dists = ds.to(Distribution, self.value_label).overlay()
            else:
                vdim = self.value_label + ' Density'
                dists = NdOverlay({0: Area([], self.value_label, vdim)},
                                  [self.group_label])
        return dists.redim(**self._redim).redim.range(**ranges).relabel(
            **self._relabel).opts(opts)
Example #7
0
 def test_op_ndoverlay_value(self):
     colors = ['blue', 'red']
     overlay = NdOverlay(
         {color: Bars(np.arange(i + 2))
          for i, color in enumerate(colors)},
         'Color').options('Bars', fill_color='Color')
     plot = bokeh_renderer.get_plot(overlay)
     for subplot, color in zip(plot.subplots.values(), colors):
         self.assertEqual(subplot.handles['glyph'].fill_color, color)
Example #8
0
 def test_op_ndoverlay_value(self):
     markers = ['d', 's']
     overlay = NdOverlay({marker: Points(np.arange(i))
                          for i, marker in enumerate(markers)},
                         'Marker').options('Points', marker='Marker')
     plot = mpl_renderer.get_plot(overlay)
     for subplot, marker in zip(plot.subplots.values(), markers):
         style = dict(subplot.style[subplot.cyclic_index])
         style = subplot._apply_transforms(subplot.current_frame, {}, style)
         self.assertEqual(style['marker'], marker)
 def test_op_ndoverlay_color_value(self):
     colors = ['blue', 'red']
     overlay = NdOverlay({color: Curve(np.arange(i))
                          for i, color in enumerate(colors)},
                         'color').options('Curve', color='color')
     plot = mpl_renderer.get_plot(overlay)
     for subplot, color in zip(plot.subplots.values(), colors):
         style = dict(subplot.style[subplot.cyclic_index])
         style = subplot._apply_transforms(subplot.current_frame, {}, style)
         self.assertEqual(style['color'], color)
Example #10
0
 def test_op_ndoverlay_value(self):
     colors = ['blue', 'red']
     overlay = NdOverlay({color: Histogram(np.arange(i+2))
                          for i, color in enumerate(colors)}, 'Color').options(
                                  'Histogram', facecolor='Color'
                          )
     plot = mpl_renderer.get_plot(overlay)
     colors = [(0, 0, 1, 1), (1, 0, 0, 1)]
     for subplot, color in zip(plot.subplots.values(),  colors):
         children = subplot.handles['artist'].get_children()
         for c in children:
             self.assertEqual(c.get_facecolor(), color)