Search code examples
plotgraphplotlyplotly-python

Why don't different bars share the same y-axis in plotly.express group bars


I made these attached group bars with plotly.express: enter image description here

And my code is like this:

import pandas as pd
df=pd.DataFrame([['C-1','C-11','111'],['C-1','C-12','121'],['C-1','C-13','31'],['C-1','C-14','120'],['C-1','C-15','100'],
      ['C-2','C-21','150'],['C-2','C-22','168'],['C-2','C-23','173'],['C-2','C-24','111'],['C-2','C-25','121'],
      ['C-3','C-31','123'],['C-3','C-32','100'],['C-3','C-33','111'],['C-3','C-34','111'],['C-3','C-35','163'],
      ['C-4','C-41','174'],['C-4','C-42','174'],['C-4','C-43','173'],['C-4','C-44','135'],['C-4','C-45','102'],
      ['C-5','C-51','118'],['C-5','C-52','122'],['C-5','C-53','113'],['C-5','C-54','178'],['C-5','C-55','142']
      ],columns=['Case group','Case number','Average revenue']) 

import plotly.express as px
fig = px.bar(df,
      x='Case number',y='Average revenue',
      color='Case group', barmode='group',text="Average revenue",
      height=500,width=700,
      color_discrete_sequence=[px.colors.qualitative.Dark2[0],px.colors.qualitative.Set2[1],px.colors.qualitative.Pastel[0],px.colors.qualitative.Set2[5],px.colors.qualitative.Set2[4],])
      )
fig.update_traces(textposition='outside')
fig.show()

My question is, how can I make the y-axis labels normally ascend so that all groups share the same y-axis scale? e.g., C-55 should be lower than C-54 in my ideal thought. Also, the annotations above each bar are too small to read. I tried to change the font size with fig.update.layout(). but it didn't work.

Please let me know if I did anything wrong.

I would really appreciate any help you can provide.


Solution

  • You only need to convert the column type to float as follows:

    df["Average revenue"] = df["Average revenue"].astype(float)
    

    The full code:

    import pandas as pd
    import plotly.express as px
    
    df=pd.DataFrame([['C-1','C-11','111'],['C-1','C-12','121'],['C-1','C-13','31'],['C-1','C-14','120'],['C-1','C-15','100'],
          ['C-2','C-21','150'],['C-2','C-22','168'],['C-2','C-23','173'],['C-2','C-24','111'],['C-2','C-25','121'],
          ['C-3','C-31','123'],['C-3','C-32','100'],['C-3','C-33','111'],['C-3','C-34','111'],['C-3','C-35','163'],
          ['C-4','C-41','174'],['C-4','C-42','174'],['C-4','C-43','173'],['C-4','C-44','135'],['C-4','C-45','102'],
          ['C-5','C-51','118'],['C-5','C-52','122'],['C-5','C-53','113'],['C-5','C-54','178'],['C-5','C-55','142']
          ],columns=['Case group','Case number','Average revenue']) 
    
    df["Average revenue"] = df["Average revenue"].astype(float)
    
    fig = px.bar(df,
          x="Case number",y='Average revenue',
          color='Case group', barmode='group',text="Average revenue",
          height=500,width=700,
          color_discrete_sequence=[px.colors.qualitative.Dark2[0],px.colors.qualitative.Set2[1],px.colors.qualitative.Pastel[0],px.colors.qualitative.Set2[5],px.colors.qualitative.Set2[4],])
    
    fig.update_traces(textposition='outside', width=0.8)
    fig.show()
    

    Output enter image description here