Home > Software design >  Remove empty bars in grouped plotly express bar chart, such that the visual distance between each gr
Remove empty bars in grouped plotly express bar chart, such that the visual distance between each gr

Time:10-29

I'm trying to group traces of a barchart and maintain the same distance between each group.

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
  "City": ["Toronto", "Toronto", "Toronto", "CPH", "CPH", "London", "London"],
  "Tower name": ["T1", "T2", "T3", "T4", "T5", "T6","T7"],
  "Height": [1.0, 1.5, 2.0, 3.0, 4.0, 2.0 ,5.0],
})

fig = px.bar(x=df['City'], y=df['Height'],color = df['Tower name'], color_discrete_sequence=['black'])
fig.update_layout(showlegend=False)
fig.update_layout(barmode='group')
fig.show()

The result is as shown below. Result

The solution I want is like this

Desired Result

I have found enter image description here

  • Related