holoviz / holoviz/hvplot

legend not working for bar plot with Plotly backend

Open
#1,035 0 comments 0 reactions 0 assignees View on GitHub
upstream
Dominant language
Python
Stars
1.4k
Forks
124
Avg merge
1d 18h
Merged PRs (30d)
1

Description

I would expect the `legend` attribute to also work for the Plotly backend in the example below. But it does not.

![image](https://user-images.githubusercontent.com/42288570/224509207-d16ce82a-19de-4361-90ab-eeae8125d08b.png)

```python
import hvplot.pandas # noqa

from bokeh.sampledata.autompg import autompg_clean as df

table = df.groupby('yr').mean(numeric_only=True)
table.head()

table = df.groupby(['yr', 'origin']).mean(numeric_only=True)
table.head()

hvplot.extension("bokeh")
table.hvplot.bar(stacked=True, height=500, legend='bottom')

hvplot.extension("plotly")
table.hvplot.bar(stacked=True, height=500, legend='bottom')
```

## HoloViews

I've tried to replicate this using HoloViews. But it seems the corresponding `legend_position` is only supported for the `matplotlib` and `bokeh` backends. There is an existing FR in HoloViews to support `legend_position` for the Plotly backend. See https://github.com/holoviz/holoviews/issues/5221

It is possible to position the Plotly legend using a hook though.

```python
import holoviews as hv # noqa

hv.extension("plotly")

from bokeh.sampledata.autompg import autompg_clean as df

table = df.groupby(['yr', 'origin']).mean(numeric_only=True)

def hook(plot, element):
# https://plotly.com/python/legend/#legend-positioning
fig = plot.state
fig["layout"]["legend"]=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01
)

plot = hv.Bars(table, kdims=["yr", "origin"], vdims="mpg").opts(stacked=True, height=500, width=1000, hooks=[hook])

import panel as pn

pn.extension()

pn.panel(plot).servable()
```

![image](https://user-images.githubusercontent.com/42288570/224530185-8c3e2e03-9609-4153-8546-5e57f7cf90ad.png)

## Additional Context

I've been working on improving the `bar` docs. As a part of this I try changing the plotting backend. I would really, really like the documentation to work for all plotting backends. That is what I would expect as a user.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.