Hi Guys,
Is it feasible to display the date instead of nothing on the x-axis of the graph?
At the moment the index of my data frame is the date.
Thanks for your help.
Best regards
Chris
Hi Guys,
Is it feasible to display the date instead of nothing on the x-axis of the graph?
At the moment the index of my data frame is the date.
Thanks for your help.
Best regards
Chris
Hi @chris_klose ,
Good question and thanks for reaching out. If Iâm reading your question right, this is a bug on our end and shouldnât be happening. Linked is an issue I just created that you can follow/comment on if youâd like.
Hereâs a potential workaround: st.line_chart is looking for an index named âindexâ so you have to rename and reset index. Thatâs not obvious and should just take index regardless of name!
An example code snippet that could help:
df = pd.DataFrame({
'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
'second column': [10, 20, 30, 40]
})
df
st.line_chart(df.rename(columns={'date':'index'}).set_index('index'))
If this doesnât help or isnât addressing your question directly, would you mind posting a code snippet so we can dive in deeper?
Hey @tc1,
thanks for replying.
Right now I get data in this format:
So I used df.reset_index(drop=True)
to get rid of the date.
The issue here is that if I leave it, I get the error you already described:
After dropping the date, I get:
st.line_chart(df["Open"]).
What Iâm actually looking for is a way to put the date on the x-axis.
Unfortunately it doesnât work for me with the above described variant.
Is there another way to display the date on the x-axis, is it a bug and will it be fixed or am I just using it in the wrong way?
Thanks for your help.
Best regards
Chris
Hi @chris_klose
Could you provide a full code snippet with what youâre trying to do, including dataframe creation, so weâre operating from the same page?
If I try the snippet provided by @tc1, Iâm able to get the dates to display on the x-axis
import streamlit as st
import pandas as pd
df = pd.DataFrame({
'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
'second column': [10, 20, 30, 40]
})
df = df.rename(columns={'date':'index'}).set_index('index')
df
st.line_chart(df)
So do I understand correctly, and the name of the index
has to be⊠âindexâ?
Hi @drorata, thatâs a good question!
You can use any name if you set it with âset_indexâ. If no name is set, Streamlit will use âindexâ and if you donât have an âindexâ column, Streamlit will display an error.
Please, let me know if this helps you.
Thanks
Hey @drorata,
As @DaniCaminos mentioned, you no longer need to rename the column. This change shipped with Streamlit version 0.51.0
import streamlit as st
import pandas as pd
df = pd.DataFrame({
'date': ['10/1/2019','10/2/2019', '10/3/2019', '10/4/2019'],
'second column': [10, 20, 30, 40]
})
df = df.set_index('date')
df
st.line_chart(df)
Hey @shubhams, could you give us a small example code which we can copy and paste to reproduce it? which streamlit version do you have?
I have the same problem. My dataframe has about 100 rows of data (date vs. new_cases_rollavg). When I do a line_chart of the data it puts the date label on the x-axis for every value. I would like to change it to plot only weeks. Iâm using Streamlit, version 0.61.0.
.
import pandas as pd
import streamlit as st# import data files
df = pd.read_csv(âdata.csvâ)# grab list of states from the dataframe
states = df.Province_State.unique()# selectbox for selecting which state to plot
state_selected = st.selectbox(âSelect state to viewâ, states, index=(53))# subselect the data for the selected state from the dataframe
df_for_display = df[df.Province_State.isin([state_selected])]# create dataframes for the rolling averages of the new cases and deaths for the state selected
df_cases_only = df_for_display[[ânew_cases_rollavgâ]]
df_deaths_only = df_for_display[[ânew_deaths_rollavgâ]]# plot the data
st.line_chart(df_cases_only)
st.line_chart(df_deaths_only)
Hi @Robb_Dunlap -
At its core, st.line_chart
is based on Altair, and there is also st.altair_chart
which provides much more customizability. This StackOverflow answer shows how to control the ticks on an Altair chart, which should help you get to what youâre looking for.
Best,
Randy
That was an involved fix but it solved my problem. I had to reset the index on my dataframe so that date is a column instead of the index. Then I used the st.altair_chart to plot the data. The Altair chart automatically plots the date ticks in a pleasant way. Thank you.
Glad to hear that it worked out. This is one of those unfortunate areas where trying to provide syntax sugar (st.line_chart
) actually makes things harder than just making the chart directly
I think streamlit shall introduce a slider for the dates or the X Axis
to zoom in and stuff
Hi @DanBrown47, welcome to the Streamlit community!
Streamlit already should support something like youâre talking about via our Altair integration. See this example for more information:
https://altair-viz.github.io/gallery/interval_selection.html
Hi guys,
You can add index-col and parse_dates paramater as below
df = pd.read_csv(dataframe, index_col=0, parse_dates=True)
st.line_chart(df[[âOpenâ, âClosedâ]])
Hello!
Did you solve the problem with axis?
Greetings!
Hi Angelicaba23,
Yes, I did it.
Greetings!
Not sure if I am too late, but I had a similar problem with this on Streamlit and this is how I fixed it:
import altair as alt
chart = alt.Chart(df).mark_line().encode(
x=alt.X('Time', axis=alt.Axis(labelOverlap="greedy",grid=False)),
y=alt.Y('ODO'))
st.altair_chart(chart, use_container_width=True)
Notice that the parameter labelOverlap
can be set to âgreedyâ to remove the ovelapping labels.
Hope this helps.
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.