# How to sorting bars inside Facet col/row Chart

**URL:** <https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808>\
**Category:** 📊 Plotly Python\
**Created:** [January 5, 2022, 4:43pm UTC](https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808 "2022-01-05T16:43:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plotKamal](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@plotKamal](https://community.plotly.com/u/plotKamal)\
**Post date:** [January 5, 2022, 4:43pm UTC](https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808/1 "2022-01-05T16:43:40Z")

</div>

Hi all,

I am trying to create faceted bar chart. Inside the faceted chart the bars are rendered in ascending or descending order.

_Code_

counting\_edu = main\_Dg.groupby([‘Education’,‘Gender’,‘Year’])[‘Numbers’].count().reset\_index()  
counting\_edu.loc[counting\_edu.Education == ‘Some college/university study without earning a bachelorâ\x80\x99s degree’,‘Education’] = ‘Without Degree’  
edu = px.bar(y=counting\_edu.Education,x=counting\_edu.Numbers,animation\_frame=counting\_edu.Year)  
edu.update\_layout(height = 500,width = 800, title = ‘Educated Likewise’,  
yaxis = {‘categoryorder’:‘total descending’})  
#edu.update\_yaxes(matches=None)  
#edu.for\_each\_yaxis(lambda yaxis: yaxis.update(showticklabels=True))  
edu.show()

_Code End_  
Is there any way to achieve a sorted bars like shown below in the each facets?

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/1/2/125a3ba6a2e44c450e0cb54efeaf28f8890c952d.png)

Currently I have the facets like below

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/c/9/c99cfb3175e64248a18b28a735c67684d8b9946a.png)

I have tried the following.

1. working with the update\_layout(xaxis ={‘categoryorder’:‘total descending’}) option.
2. tried the category\_order option in the px.bar() function.

Then I realized that the y-axis are common between multiple facets, so tried  
3) fig.update\_yaxes(matches=None)

Any ideas or thoughts would be greatly helpful.

---

<div class="post-metadata">

**Author:** ![plotme1](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@plotme1](https://community.plotly.com/u/plotme1)\
**Post date:** [January 5, 2022, 5:57pm UTC](https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808/2 "2022-01-05T17:57:55Z")

</div>

I think you would want to sort the data source first – something like

```auto
df = df.sort_values('count_variable_in_your_chart',ascending=True)

```

---

<div class="post-metadata">

**Author:** ![plotKamal](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@plotKamal](https://community.plotly.com/u/plotKamal)\
**Post date:** [January 6, 2022, 1:27am UTC](https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808/3 "2022-01-06T01:27:44Z")

</div>

Thanks for looking into my challenge.

I tried the sorting the variables already. It is not providing the intended behaviour.

I am thinking of going with seperate graph objects bar charts, with subplots. I wanted to learn if plotly express has any option that I have missed.

---

<div class="post-metadata">

**Author:** ![remsidave](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@remsidave](https://community.plotly.com/u/remsidave)\
**Post date:** [November 28, 2023, 3:00pm UTC](https://community.plotly.com/t/how-to-sorting-bars-inside-facet-col-row-chart/59808/4 "2023-11-28T15:00:24Z")

</div>

Had the same issue, it is due to the fact the both plots share the y axis per default. if you sort your data first

`df = df.sort_values('count_variable_in_your_chart',ascending=True)`

and then facet plot with .update\_yaxes(matches=None) as you had commented out it should give you teh intended behaviour. It worked at least for me  
`(  
px.bar(y=counting\_edu.Education,x=counting\_edu.Numbers,animation\_frame=counting\_edu.Year)

.update\_layout(height = 500,width = 800, title = ‘Educated Likewise’,yaxis = {‘categoryorder’:‘total descending’})

.update\_yaxes(matches=None)  
)`
