I have created a stacked bar chart in using plotly express that visualises edits made to a files in GDrive, and who made those edits.
My source dataframe has columns
- file_path
- emailAddress (of the person editing the file)
- tally
I have file_path plotted on the x axis.
The problem I’m having is that I want all the bars to appear in descending order of edits, regardless of who made those edits. As you can see that’s not happening right now.
Here is my code:
import plotly.graph_objects as go
fig = go.Figure()
# Get a list of unique email addresses to create one trace per email address
email_addresses = df['emailAddress'].unique()
# Create a trace for each email address
for email in email_addresses:
df_filtered = df[df['emailAddress'] == email]
fig.add_trace(go.Bar(x=df_filtered['path_name'], y=df_filtered['tally'], name=email))
# Update the layout to stack the bars
fig.update_layout(barmode='stack', xaxis_title="File path", yaxis_title="Tally", title="Tally by file path and Email Address")
fig.show()
Can anyone suggest how I change this to get all bars appearing in descending order of edits, regardless of who made the edits.
You need to specify the xaxis categoryorder
.
Set
categoryorder
to “total ascending” or “total descending” if
order should be determined by the numerical order of the values.
fig.update_xaxes(categoryorder='total descending')
See Bar Chart with Sorted or Ordered Categories.
1