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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 21 (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.

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

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)

Im using plotly to make the plots instead of builtin function

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.

same problem !

same problem

same problem.

Same Issue…