Ordering secondary Y axis Bar Graph

Hello guys, it’s my first post here.
I’ve been working on this for a week now, and couldn’t solve my problem yet, so here I am.

I am using the dataframe below to plot a bar graph, however I’m using a multicategorical Y-axis graph.

The display graph is not being displayed as I desired - I already used categoryorder commands, but no luck yet.

Here is a part of my dataframe:
image

Here is my code:

Here is how the graph is being ploted:
ploted1

I want the graph to plot with the higher amount on top for each category. Here is how I need it:
ploted2

Any thoughts on how I could solve it?
Thanks in advance :slight_smile:

I saw that many people asked about this problem so it maybe a problem of Plotly. And I think you can use subplots to make it.

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import numpy as np

df = pd.read_excel('Dtaa.xlsx')
df = df.sort_values(['PALAVRA', 'QTD_EVENTOS'], ascending=[True, True]).groupby('PALAVRA').head(10)
df3= df[df['PALAVRA']=='ponte rolante']
df4= df[df['PALAVRA']=='motorista camihao']
df5= df[df['PALAVRA']=='carga suspensa']

fig1=go.Figure(go.Bar(x=df3['QTD_EVENTOS'], 
                      y=[df3['PALAVRA'],
                         df3['ASSOCIADO']],
                      orientation='h',
                      textposition='outside',
                      text=df3['QTD_EVENTOS'],
                      marker_color='#c0c0c0'))
fig1.update_layout(autosize=True,
                   plot_bgcolor='rgb(250,250,250)')

fig2=go.Figure(go.Bar(x=df4['QTD_EVENTOS'], 
                      y=[df4['PALAVRA'],
                         df4['ASSOCIADO']],
                      orientation='h',
                      textposition='outside',
                      text=df4['QTD_EVENTOS'],
                      marker_color='#c0c0c0'))
fig2.update_layout(autosize=True,
                   plot_bgcolor='rgb(250,250,250)')

fig3=go.Figure(go.Bar(x=df5['QTD_EVENTOS'], 
                      y=[df5['PALAVRA'],
                         df5['ASSOCIADO']],
                      orientation='h',
                      textposition='outside',
                      text=df5['QTD_EVENTOS'],
                      marker_color='#c0c0c0'))
fig3.update_layout(autosize=True,
                   plot_bgcolor='rgb(250,250,250)')

fig4 = make_subplots(rows=3, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.02)

fig4.append_trace(fig1.data[0], row=1, col=1)
fig4.append_trace(fig2.data[0], row=2, col=1)
fig4.append_trace(fig3.data[0], row=3, col=1)
fig4.update_layout(height=600)
fig4.show()

Hope this help. And if you asked next time, please post code, not image.

2 Likes