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)
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 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.
I show the table and the chart using this code:
Output:
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
stsyntax would.I used
st.writeas 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 forst.altair_chart()as per st.altair_chart documentation and passeduse_container_width=Trueand that gave me the scaling behavior I was looking for.My code for reference:
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
plotlyto make the plots instead of builtin functionsame 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)
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 Vote for this.