Histogram and bar charts only hides first or last bars when clicking legend

I’m drawing multiple histograms or a bar charts using a loop. I need to make multiple figures to be able to set the legendgroup for each.
When I click on the legend to hide the top or bottom category it removes it entirely. This is what I want.
When I click to hide any of the categories in the middle, the bars disappear but they still leave a space for that category.

Can I make it so that the space is always removed along with the bar?

Code:

fig = go.Figure()

for subcategory in subcategory_order:
    subcategory_df = df.loc[df['subcategory'] == subcategory]

    fig.add_trace(go.Histogram(y=subcategory_df['subcategory'],
                               name=subcategory,
                               showlegend=True,
                               legendgroup=subcategory
                              ))
    
fig.update_traces(orientation='h')
fig.show()

Result:

Hi,

I believe plotly maps internally each category into a value to create the bar plot, so there is no way to get rid of this space. The only reason why this works on the edges is because autoscale=True.

Thanks for the reply. It’s odd that it works like that for bar plots but for box plots it removes the category like I’m hoping for (I’m making a subplot of a box plot and a bar plot with a shared category axes)

I cheated my way to this by making a bar plot out of a box plot. But once I made a subplot using that and one other boxplot the same happened. I couldn’t get the categories to disappear completely when hidden. So I guess the shared_yaxes=True variable for subplots causes the same behavior

1 Like