Skip to content

Commit

Permalink
Add color to bars for Plotly (#6294)
Browse files Browse the repository at this point in the history
Co-authored-by: maximlt <[email protected]>
  • Loading branch information
hoxbro and maximlt committed Jul 4, 2024
1 parent 4f20762 commit d0d535b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions holoviews/plotting/plotly/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ class BarPlot(BarsMixin, ElementPlot):
show_legend = param.Boolean(default=True, doc="""
Whether to show legend for the plot.""")


style_opts = ['visible']
style_opts = ['visible', 'color']

selection_display = PlotlyOverlaySelectionDisplay()

Expand Down Expand Up @@ -282,6 +281,12 @@ def get_data(self, element, ranges, style, **kwargs):

return bars

def graph_options(self, element, ranges, style, **kwargs):
if 'color' in style:
style['marker_color'] = style.pop('color')
opts = super().graph_options(element, ranges, style, **kwargs)
return opts

def init_layout(self, key, element, ranges, **kwargs):
layout = super().init_layout(key, element, ranges)
stack_dim = None
Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/plotting/plotly/test_barplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,10 @@ def test_bar_group_stacked(self):
plot = self._get_plot_state(bars)
np.testing.assert_equal(set(plot['data'][0]['x']), set(pets))
np.testing.assert_equal(plot['data'][0]['y'], np.array([8, 7, 6, 7]))

def test_bar_color(self):
data = pd.DataFrame({"A": range(5)})
bars = Bars(data).opts(color="gold")
fig = self._get_plot_state(bars)
data = fig["data"][0]
assert data["marker"]["color"] == "gold"

0 comments on commit d0d535b

Please sign in to comment.