holoviz / hvplot

A high-level plotting API for pandas, dask, xarray, and networkx built on HoloViews
https://hvplot.holoviz.org
BSD 3-Clause "New" or "Revised" License
1.15k stars 108 forks source link

Let color (of line plot) support dict values #831

Open MarcSkovMadsen opened 2 years ago

MarcSkovMadsen commented 2 years ago

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

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.

philippjfr commented 2 years ago

Agreed, I like this.