Sorting two plotly bar charts by column value

Question:

Let’s suppose this is my dataset:

my_data = pd.DataFrame.from_dict({
  'Countries':['Australia', 'Canada', 'China', 'Netherlands'],
  'A':[1.89,1.45,1.22,0.94],
  'B':[0.8,1,1.45,1.6]
})

I have this two bar plots:
enter image description here

I want to order the countries by the value of ‘A’ in both plots, but I tried a lot of things and just can’t. I will be very grateful for any help.

This is my code:

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

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Bar( 
      x=my_data['A'], 
      y=my_data['Countries'], 
      orientation='h', 
      text = my_data['A'], 
      name = 'A'),
    row=1, col=1
)

fig.add_trace(
    go.Bar( 
      x=my_data['B'], 
      y=my_data['Countries'], 
      orientation='h', 
      text = my_data['B'], 
      name = 'B'),
    row=1, col=2
)

fig.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'})
fig.update_layout(height=600, width=1000, title_text="Side By Side Subplots")
fig.show()
Asked By: soy nicolas

||

Answers:

There may be a more plotly-specific way to do this, but you can use fig.update_yaxes(categoryorder='array', categoryarray=desired_order) to update all the subplot y axes, where desired_order is a list to use for ordering. So if you want to order by column A, desired_order = my_data.sort_values('A')['Countries'].to_list(). All together:

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

my_data = pd.DataFrame.from_dict({
  'Countries':['Australia', 'Canada', 'China', 'Netherlands'],
  'A':[1.89,1.45,1.22,0.94],
  'B':[0.8,1,1.45,1.6]
})

desired_order = my_data.sort_values('A')['Countries'].to_list()
print(desired_order)

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Bar( 
      x=my_data['A'], 
      y=my_data['Countries'], 
      orientation='h', 
      text = my_data['A'], 
      name = 'A'),
    row=1, col=1
)



fig.add_trace(
    go.Bar( 
      x=my_data['B'], 
      y=my_data['Countries'], 
      orientation='h', 
      text = my_data['B'], 
      name = 'B'),
    row=1, col=2
)


fig.update_yaxes(categoryorder='array', categoryarray=desired_order)
fig.update_layout(barmode='stack')
fig.update_layout(height=600, width=1000, title_text="Side By Side Subplots")
fig.show()

produces

plot bar subplot axis ordering

I couldn’t get the same behavior using just update_layout, not sure if that’s a bug or a usage issue (I don’t know plotly super well).

Edit: I figured out how to use update_layout. As noted in this thread: https://community.plotly.com/t/fig-update-layout-only-affecting-first-subplot/29648 , yaxis is only the first subplot, you can specify yaxis2 to reference the second subplot. So if you want to use update_layout (and not update_yaxes), you could do the following:

fig.update_layout(barmode='stack', yaxis={'categoryorder':'array', 'categoryarray':desired_order},
                  yaxis2={'categoryorder':'array', 'categoryarray':desired_order})

Answered By: chris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.