Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add value_labels option to bar plots #496

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jsignell
Copy link
Member

Closes #375

Example:

# import libraries
import numpy as np
import pandas as pd
import hvplot.pandas

# create some sample data
df = pd.DataFrame({
    'col1': ['bar1', 'bar2', 'bar3'] * 2,
    'col2': np.random.rand(6),
    'col3': ['apple',] * 3 + ['orange'] * 3
})

# plot
df[df.col3 == 'apple'].hvplot(kind='bar', x='col1', y='col2', ylim=(0, 1.2), value_labels=True)

image

NOTE: This doesn't work for cases where the data are grouped along more than one dimension on the x axis. AFAICT labeling those isn't possible in holoviews. For instance:

df.hvplot(kind='bar', x='col1', y='col2', ylim=(0, 1.2), by="col3")

image

df.hvplot(kind='bar', x='col1', y='col2', ylim=(0, 1.2), by="col3", value_labels=True)
------------------------------------------------------------------
ValueError                       Traceback (most recent call last)
<ipython-input-24-b9ea1072518c> in <module>
----> 1 df.hvplot(kind='bar', x='col1', y='col2', ylim=(0, 1.2), by="col3", value_labels=True)

~/holoviz/hvplot/hvplot/plotting/core.py in __call__(self, x, y, kind, **kwds)
     70                 return pn.panel(plot, **panel_dict)
     71 
---> 72         return self._get_converter(x, y, kind, **kwds)(kind, x, y)
     73 
     74     def _get_converter(self, x=None, y=None, kind=None, **kwds):

~/holoviz/hvplot/hvplot/converter.py in __call__(self, kind, x, y)
   1023                     dataset = Dataset(data)
   1024                     dataset = dataset.redim(**self._redim)
-> 1025                 obj = method(x, y)
   1026                 obj._dataset = dataset
   1027 

~/holoviz/hvplot/hvplot/converter.py in bar(self, x, y, data)
   1388             if self.value_labels:
   1389                 labels = Labels(obj.data, obj.kdims + obj.vdims, obj.vdims).opts(text_baseline="bottom")
-> 1390                 return obj * labels
   1391             return obj
   1392         return self._category_plot(Bars, x, list(y), data)

~/conda/envs/dask-dev/lib/python3.7/site-packages/holoviews/core/data/__init__.py in __init__(self, data, kdims, vdims, **kwargs)
    351                                            datatype=kwargs.get('datatype'))
    352         (data, self.interface, dims, extra_kws) = initialized
--> 353         super(Dataset, self).__init__(data, **dict(kwargs, **dict(dims, **extra_kws)))
    354         self.interface.validate(self, validate_vdims)
    355 

~/conda/envs/dask-dev/lib/python3.7/site-packages/holoviews/core/dimension.py in __init__(self, data, kdims, vdims, **params)
    847             params['cdims'] = {d if isinstance(d, Dimension) else Dimension(d): val
    848                                for d, val in params['cdims'].items()}
--> 849         super(Dimensioned, self).__init__(data, **params)
    850         self.ndims = len(self.kdims)
    851         cdims = [(d.name, val) for d, val in self.cdims.items()]

~/conda/envs/dask-dev/lib/python3.7/site-packages/holoviews/core/dimension.py in __init__(self, data, id, plot_id, **params)
    506             params['group'] = long_name
    507 
--> 508         super(LabelledData, self).__init__(**params)
    509         if not util.group_sanitizer.allowable(self.group):
    510             raise ValueError("Supplied group %r contains invalid characters." %

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/parameterized.py in __init__(self, **params)
   2338 
   2339         self.param._generate_name()
-> 2340         self.param._setup_params(**params)
   2341         object_count += 1
   2342 

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/parameterized.py in override_initialization(self_, *args, **kw)
    989         original_initialized=parameterized_instance.initialized
    990         parameterized_instance.initialized=False
--> 991         fn(parameterized_instance,*args,**kw)
    992         parameterized_instance.initialized=original_initialized
    993     return override_initialization

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/parameterized.py in _setup_params(self_, **params)
   1200                 self.param.warning("Setting non-parameter attribute %s=%s using a mechanism intended only for parameters",name,val)
   1201             # i.e. if not desc it's setting an attribute in __dict__, not a Parameter
-> 1202             setattr(self,name,val)
   1203 
   1204     @classmethod

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/parameterized.py in _f(self, obj, val)
    294             instance_param.__set__(obj, val)
    295             return
--> 296         return f(self, obj, val)
    297     return _f
    298 

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/parameterized.py in __set__(self, obj, val)
    821             val = self.set_hook(obj,val)
    822 
--> 823         self._validate(val)
    824 
    825         _old = NotImplemented

~/conda/envs/dask-dev/lib/python3.7/site-packages/param/__init__.py in _validate(self, val)
   1381             if min_length is not None and max_length is not None:
   1382                 if not (min_length <= l <= max_length):
-> 1383                     raise ValueError("%s: list length must be between %s and %s (inclusive)"%(self.name,min_length,max_length))
   1384             elif min_length is not None:
   1385                 if not min_length <= l:

ValueError: kdims: list length must be between 2 and 2 (inclusive)

@jsignell
Copy link
Member Author

I'm happy to fix this up if it is something that hvplot would like to support.

@jbednar
Copy link
Member

jbednar commented Oct 18, 2021

I'd love to see that supported!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add value labels of your data to hvplot by using parameter instead of hv.Labels()
2 participants