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)
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)
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)
def test_quadmesh_update_cbar(self): xs = ys = np.linspace(0, 6, 10) zs = np.linspace(1, 2, 5) XS, YS, ZS = np.meshgrid(xs, ys, zs) values = np.sin(XS) * ZS ds = Dataset((xs, ys, zs, values.T), ['x', 'y', 'z'], 'values') hmap = ds.to(QuadMesh).options(colorbar=True, framewise=True) plot = mpl_renderer.get_plot(hmap) self.assertEqual(plot.handles['cbar'].get_clim(), (-0.9989549170979283, 0.9719379013633127)) plot.update(3) self.assertEqual(plot.handles['cbar'].get_clim(), (-1.7481711049213744, 1.7008913273857973))
def test_quadmesh_update_cbar(self): xs = ys = np.linspace(0, 6, 10) zs = np.linspace(1, 2, 5) XS, YS, ZS = np.meshgrid(xs, ys, zs) values = np.sin(XS) * ZS ds = Dataset((xs, ys, zs, values.T), ['x', 'y', 'z'], 'values') hmap = ds.to(QuadMesh).options(colorbar=True, framewise=True) plot = mpl_renderer.get_plot(hmap) self.assertEqual(plot.handles['cbar'].get_clim(), (-0.9989549170979283, 0.9719379013633127)) plot.update(3) self.assertEqual(plot.handles['cbar'].get_clim(), (-1.7481711049213744, 1.7008913273857973))
def hist(self, x, y, data=None): data, x, y = self._process_args(data, x, y) labelled = ['y'] if self.invert else ['x'] plot_opts = dict(self._plot_opts, labelled=labelled) opts = dict(plot=plot_opts, style=self._style_opts, norm=self._norm_opts) hist_opts = { 'bin_range': self.kwds.get('bin_range', None), 'normed': self.kwds.get('normed', False), 'cumulative': self.kwds.get('cumulative', False) } if 'bins' in self.kwds: bins = self.kwds['bins'] if isinstance(bins, int): hist_opts['num_bins'] = bins else: hist_opts['bins'] = bins if not isinstance(y, (list, tuple)): if self.stacked and not self.subplots and not 'bin_range' in self.kwds: ys = data[y] hist_opts['bin_range'] = (ys.min(), ys.max()) ds = Dataset(data, self.by, y) hist = hists = histogram(ds.to(Dataset, [], y, self.by), **hist_opts) if self.by: hist = hists.last hists = hists.layout() if self.subplots else hists.overlay() ranges = { hist.kdims[0].name: self._dim_ranges['x'], hist.vdims[0].name: self._dim_ranges['y'] } return hists.opts({ 'Histogram': opts }).redim(**self._redim).redim.range(**ranges) ds = Dataset(data) hists = [] for col in y: hist = histogram(ds, dimension=col, **hist_opts) ranges = { hist.kdims[0].name: self._dim_ranges['x'], hist.vdims[0].name: self._dim_ranges['y'] } hists.append((col, hist.redim.range(**ranges).relabel( **self._relabel).opts(**opts))) return self._by_type(hists, sort=False).redim(**self._redim)