RustyShackleford
RustyShackleford

Reputation: 3677

How to remove rempty duplicate dates from line chart in streamlit via plotly?

I have the below line graph:

enter image description here

by this code:

import streamlit as st
import plotly.express as px
import pandas as pd

df = pd.DataFrame()
fig_col1, fig_col2,fig_col3 = st.columns(3)

with fig_col1:
    st.markdown("### First Chart")
    fig = px.line(df, y='revenue', x="calendar_date")
    fig.update_layout(xaxis=dict(tickformat="%m-%d"))
    st.plotly_chart(fig, use_container_width=True)

How do I remove the dupilcate date values? The data is not repeating any dates, but the empty date comes up. When I hover over say the second 07-20, there is no data presented. I have no idea why this is coming up.

I want the x-axis date to be only 1 of each. For example 07-20 should only write once.

Upvotes: 1

Views: 504

Answers (1)

Derek O
Derek O

Reputation: 19610

This isn't happening because there are duplicate dates – it looks like the datetimes in your data are occurring on 7-20, 7-21, and 7-22, but the default zoom setting for the chart is such that there is an additional tick mark between 7-20 and 7-21 occurring at 7-20-22 12:00:00, but because tickformat="%m-%d" shows only the month and day, the hour is being cut off.

Depending on what you want to see, you can set tickformat="%m-%d-%h" so that the hour is visible too.

Or if you only want to see the day no matter how far you zoom in, you can force the tickmarks to be 1 day apart using fig.update_xaxes(dtick="D1"). Some further documentation on dtick can be found here.

Upvotes: 3

Related Questions