hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

Let color (of line plot) support dict values

Open MarcSkovMadsen opened this issue 3 years ago • 1 comments

Request

Enable using a dict for color in line plots and what ever other types of plots where it makes sense.

Motivation

I'm trying to improve the docstrings of hvPlot.

As the

  • reference guide and docstrings is very sparse
  • the code is hard to follow in an editor
  • the code is not type annotated

It is very hard to figure out which types are supported.

So instead I assume hvPlot supports the same api and types as Pandas .plot.

Now I want to document the basic by and color. I cannot find any documentation in the docstrings, reference guide or with hvplot.help('line').

So I look up https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.line.html and can see that they support color to be of type dict. The only way I can figure out if this is supported in hvPlot is to try it out.

It fails

import hvplot.pandas

from bokeh.sampledata.iris import flowers as df
plot = df.hvplot.scatter(
    x='sepal_length', y='sepal_width', by='species', 
    legend='top', height=400, width=400,
    color={"setosa": "pink"}
)
TypeError: unhashable type: 'dict'
Traceback (most recent call last):
  File "/opt/conda/lib/python3.9/site-packages/bokeh/application/handlers/code_runner.py", line 231, in run
    exec(self._code, module.__dict__)
  File "/home/jovyan/repos/private/hvplot/script.py", line 4, in <module>
    plot = df.hvplot.scatter(
  File "/home/jovyan/repos/private/hvplot/hvplot/plotting/core.py", line 300, in scatter
    return self(x, y, kind='scatter', **kwds)
  File "/home/jovyan/repos/private/hvplot/hvplot/plotting/core.py", line 85, in __call__
    return self._get_converter(x, y, kind, **kwds)(kind, x, y)
  File "/home/jovyan/repos/private/hvplot/hvplot/converter.py", line 1228, in __call__
    obj = method(x, y)
  File "/home/jovyan/repos/private/hvplot/hvplot/converter.py", line 1641, in scatter
    return self.chart(Scatter, x, y, data)
  File "/home/jovyan/repos/private/hvplot/hvplot/converter.py", line 1599, in chart
    return self.single_chart(element, x, y, data)
  File "/home/jovyan/repos/private/hvplot/hvplot/converter.py", line 1488, in single_chart
    if 'color' in opts_ and opts_['color'] in data.columns:
  File "/opt/conda/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 4572, in __contains__
    hash(key)
TypeError: unhashable type: 'dict'

I believe this should work because its powerful and users would expect it to work as it works for the reference Pandas .plot implementation.

MarcSkovMadsen avatar Aug 09 '22 05:08 MarcSkovMadsen

Agreed, I like this.

philippjfr avatar Aug 09 '22 09:08 philippjfr