N.Woodruff
N.Woodruff

Reputation: 133

How to add a gap between specific bars in Plotly

I have the following bar chart generated from px.bar(...) with Plotly Express: Bar chart

I'd like to add a vertical gap between the 10 and "All" bars to differentiate the "All" bar. Is there a way to do this in Plotly?

Upvotes: 3

Views: 1078

Answers (1)

vestland
vestland

Reputation: 61084

To my knowledge there's no direct way to do this. As an alternative solution I would consider splitting the data and setting your figure up as two subplots. You'll need to add some customization like shared_xaxes=True and adjust row heights to suit your use case through row_heights=[0.2, 0.8]. Using the built-in dataset px.data.tips the snippet below uses an approach like this to produce the following plot:

enter image description here

Complete code:

import plotly.express as px
from plotly.subplots import make_subplots
from itertools import cycle

df = px.data.tips()

fig1 = px.bar(df[df.day != 'Sun'], x="total_bill", y="day", orientation='h')
fig2 = px.bar(df[df.day == 'Sun'], x="total_bill", y="day", orientation='h')

f = fig2.full_figure_for_development(warn=False)

fig = make_subplots(rows=2, cols=1, shared_xaxes=True, row_heights=[0.2, 0.8])
fig.update_layout(template = 'plotly_white')
fig.update_xaxes(showgrid = False)

fig.add_traces(fig2.data, 1, 1)
fig.add_traces(fig1.data, 2,1)
fig.show()

Upvotes: 3

Related Questions