Date at x-axis of line_chart

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

1 Like

Hi @chris_klose :wave:,

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?

1 Like

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:


and with it the display works with 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)
1 Like

How do I fix the x axis so it only shows every 100th date

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.
Screen Shot 2020-06-12 at 3.32.51 PM.

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 :frowning: