Sort the bar chart in descending order

Hello,

I am trying to plot the below dataframe using bar_chart function.
image

How can I display the chart in the same descending order as given above? Why is it shuffling the sequence? I am able to do this using matplotlib. But I want to plot it using streamlit barchart.

Can you please help?

Thanks,
Aditya

1 Like

Hi, @adi.kadrekar. For this you might want to use Altair charts directly. For example:

import streamlit as st
import pandas as pd
import altair as alt

data = pd.DataFrame({
    'city': ['Cincinnati', 'San Francisco', 'Pittsburgh'],
    'sports_teams': [6, 8, 9],
})

st.write(data)
st.write(alt.Chart(data).mark_bar().encode(
    x=alt.X('city', sort=None),
    y='sports_teams',
))

which produces:

Btw, just noticed that this bug has already been filed by another user in Github. Thanks for noticing this, @adi.kadrekar!

Thank you so much @Adrien_Treuille!
This is really helpful.

1 Like