From 619989fef4a1d6bce1ebec6ae2ca1225e68c2657 Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Tue, 28 Jul 2020 10:43:08 -0400 Subject: [PATCH 1/2] Add value_labels option to bar plots --- hvplot/converter.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/hvplot/converter.py b/hvplot/converter.py index 7e24f661c..7d50249a3 100644 --- a/hvplot/converter.py +++ b/hvplot/converter.py @@ -262,7 +262,7 @@ class HoloViewsConverter(object): 'polygons' : ['logz', 'c'], 'labels' : ['text', 'c', 'xoffset', 'yoffset', 'text_font', 'text_font_size'], 'kde' : ['bw_method', 'ind', 'bandwidth', 'cut', 'filled'], - 'bivariate': ['bandwidth', 'cut', 'filled', 'levels'] + 'bivariate': ['bandwidth', 'cut', 'filled', 'levels'], } _kind_mapping = { @@ -311,7 +311,7 @@ def __init__(self, data, x, y, kind=None, by=None, use_index=True, x_sampling=None, y_sampling=None, project=False, tools=[], attr_labels=None, coastline=False, tiles=False, sort_date=True, check_symmetric_max=1000000, - **kwds): + value_labels=False, **kwds): # Process data and related options self._redim = fields @@ -375,6 +375,7 @@ def __init__(self, data, x, y, kind=None, by=None, use_index=True, # Process options self.stacked = stacked + self.value_labels = value_labels plot_opts = dict(self._default_plot_opts, **self._process_plot()) @@ -1219,6 +1220,7 @@ def single_chart(self, element, x, y, data=None): if element is Bars and not self.subplots: if any(y in self.indexes for y in ys): data = data.reset_index() + print(data) return (element(data, ([x] if x else [])+self.by, ys) .relabel(**self._relabel) .redim(**self._redim) @@ -1269,7 +1271,7 @@ def _process_chart_args(self, data, x, y, single_y=False, categories=None): y = self._process_chart_y(data, x, y, single_y) # sort by date if enabled and x is a date - if x is not None and self.sort_date and self.datatype == 'pandas': + if x is not None and not isinstance(x, list) and self.sort_date and self.datatype == 'pandas': from pandas.api.types import is_datetime64_any_dtype as is_datetime if x in self.indexes: index = self.indexes.index(x) @@ -1383,11 +1385,15 @@ def bar(self, x=None, y=None, data=None): data, x, y = self._process_chart_args(data, x, y, categories=self.by) if (x or self.by) and y and (self.by or not isinstance(y, (list, tuple) or len(y) == 1)): y = y[0] if isinstance(y, (list, tuple)) else y - return self.single_chart(Bars, x, y, data) + obj = self.single_chart(Bars, x, y, data) + if self.value_labels: + labels = Labels(obj.data, obj.kdims + obj.vdims, obj.vdims).opts(text_baseline="bottom") + return obj * labels + return obj return self._category_plot(Bars, x, list(y), data) def barh(self, x=None, y=None, data=None): - return self.bar(x, y, data).opts('Bars', invert_axes=True) + return self.bar(x, y, data).opts('Bars', invert_axes=True).opts("Labels", text_align="left", text_baseline="middle") ########################## # Statistical charts # @@ -1608,7 +1614,14 @@ def labels(self, x=None, y=None, data=None): self.use_index = False data, x, y = self._process_chart_args(data, x, y, single_y=True) - text = self.kwds.get('text', [c for c in data.columns if c not in (x, y)][0]) + text = self.kwds.get('text', None) + if text is None: + unused_cols = [c for c in data.columns if c not in (x, y)] + if len(unused_cols) > 0: + text = unused_cols[0] + else: + text = y + kdims, vdims = self._get_dimensions([x, y], [text]) opts = self._get_opts('Labels') return Labels(data, kdims, vdims).redim(**self._redim).opts(**opts) From 3f621cc0dbac5a65ad4f352d020f70f05e57863f Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Tue, 28 Jul 2020 10:54:42 -0400 Subject: [PATCH 2/2] Get rid of errant print data --- hvplot/converter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hvplot/converter.py b/hvplot/converter.py index 7d50249a3..24394835a 100644 --- a/hvplot/converter.py +++ b/hvplot/converter.py @@ -1220,7 +1220,6 @@ def single_chart(self, element, x, y, data=None): if element is Bars and not self.subplots: if any(y in self.indexes for y in ys): data = data.reset_index() - print(data) return (element(data, ([x] if x else [])+self.by, ys) .relabel(**self._relabel) .redim(**self._redim)