streamlit: streamlit.bar_chart should not sort columns.

Summary

streamlit.bar_chart(some_data_frame) will sort index values of some_data_frame before displaying. This prevents users from sorting by e.g. values.

Steps to reproduce

streamlit.bar_chart(some_data_frame) with a DataFrame that isn’t sorted by columns.

Expected behavior:

Streamlit to maintain sort order.

Actual behavior:

Streamlit sorts by column.

Is this a regression?

No

Debug info

  • Streamlit version: Streamlit, version 0.47.4
  • Python version: Python 3.7.4
  • PyEnv

Related:

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 7
  • Comments: 28 (2 by maintainers)

Most upvoted comments

Experiencing the same issue here. Not sure how this could be considered not a bug. There is no reason that every bar chart should be sorted by their index column as it severely limits the usability of the built in bar_chart. The API also has not been updated in any way to reflect this unusual behavior in the 4 years since this bug was closed.

the issue is remained after 5 years, Please provide the option: sort_X=False)

the issue is remained after 5 years, Please provide the option: sort_X=False)

The issue is closed, so I doubt anyone will solve it. Streamlit is now in the hands of Snowflake, right? Maybe we could start pushing Snowflake through their customer portal, WDYT?

Hi @dcaminos

I believe the original bug report is correct. Let’s consider the following example. I have a dataframe with the following data. It has already been sorted according to the average_salary column.

               name  average_salary
0           Finance          7500.0
1         Marketing          6633.0
2  Customer Service          5350.0
3             Sales          5267.0
4       Engineering          5100.0

I show the table and the chart using this code:

st.dataframe(df)
st.bar_chart(df, x=df.columns[0])

Output: image

As you can see, the chart sorts the x axis alphabetically, leading to inconsistent results. The expectation is that it should leave the order of the elements untouched. Please reopen the bug if you agree.

Just a heads up to anyone using the Altair workaround and trying to get the chart to use the full width of the container like a regular st syntax would.

I used st.write as per workaround, which would produce an Altair chart of width 300px. Following Altair docs I tried setting `.properties(width=‘container’) but this made the chart disappear.

Instead of st.write() I swapped it out for st.altair_chart() as per st.altair_chart documentation and passed use_container_width=True and that gave me the scaling behavior I was looking for.

My code for reference:

st.altair_chart(alt.Chart(df).mark_bar().encode(
    x=alt.X('weekday', sort=None, title="Day of week"),
    y=alt.Y('count', title="Count"),
), use_container_width=True)

same but it seems to me that it’s not a bug, but feature, bad feature. It would be very nice if developers add to bar_chart argument ‘order’, that will change order of bars

Reopening this. Sorry for not seeing all the comments, closed issues can be a bit hard to see. Thanks to @EtheriVR for bringing this to my attention!

Also marking this is an enhancement since the current behavior is expected. It seems like this is an Altair default but might make sense to change it if it’s confusing.

Related: #7111

This problem is really putting me off from using streamlit. Every time I want to make a bar chart I have to do an altair workaround which is time consuming and way more difficult… It should not sort alphabetically dude…

Im using plotly to make the plots instead of builtin function

same issue

same problem !

same problem

same problem.

Same Issue…

This is a workaround I’ve used to achieve a correct sort where the column names are intervallic numbers/decimals. (Not a solution for strings in general)

  1. Create a dedicated display_[dataframe] as a preprocessing step
  2. Map the numerical column leveraging rjust to add leading spaces to the column format. This tricks the lexicographic sort bug/feature to approximate a numerical sort.

display_interest_rate_column_formatted = slider_values_df['interest_rate'].map(lambda x: str(x)).map(lambda x: x[0:x.index(".")].rjust(4," ") + x[x.index("."):len(x)].ljust(4,"0") + "%")

  1. Use st.bar_chart with display_[dataframe]

+1 Vote for this.