How to change the order in x-axis

I want to have some pics through the command st.line_chart, but i had the x-axis in the order of HV10,HV120,HV15,HV20,HV30,HV5.
And l guess itโ€™s in the order of the third letter which i can see 10, 120, 150.
How can l change the order into the correct column order like the pics i attached below?

Thanks a lot.
Yours, George

1

what tool do you use for plotting, you can use matplotlib, altair and other packages to plot this plot with little code and many possibilities

i use set_index to make HV as the index column and st.line_chart to plot.
I want the x-axis in the order like HV5,HV10,HV15,but not in the order HV10, HV120(itโ€™s in the order of the every number),so how can i do it?

Itโ€™s more quick to ask this question on google, this is not a problem of streamlit, different package use different method.

I am using Streamlitโ€™s line graph and having this problem when I use set_index. It sorts alphabetically: April โ†’ Jan โ†’ May etc. instead of how I need it to be in order of time.

Do someone know how to plot x axis with month on non alphabetical order? i.e., instad Jan, Feb etc

@julia_Larsson ,

st.line_chart is built on altair / vegalite. I would suggest looking at something like this:

points = alt.Chart(c).mark_circle(size = 0).encode(color = 'company:N')

lines = alt.Chart(c, title = 'Sample'.upper()).mark_line().encode(
                 x = alt.X('index:T', axis = alt.Axis(title = 'Date'.upper(), format = ("%b"))),
                 y = alt.Y('value:Q', axis = alt.Axis(title = 'Value'.upper())),
                 color = alt.Color('company:N', legend = alt.Legend(title = 'Legend')),
            ).interactive()

chart = lines + points

st.altair_chart(chart)

The main significant code I would guess you would want to pay attention to is:

x = alt.X('index:T', axis = alt.Axis(title = 'Date'.upper(), format = ("%b"))),

where the format is based on these docs: GitHub - d3/d3-time-format: Parse and format times, inspired by strptime and strftime..

Let me know if this helps.

1 Like