0

Excel File

Now I made a dashboard where you can select which measurement among temp, voc, humidity and others to chart and the tower_id too. Now I plan to make a multi-line plot where each line will be a date selected in the date multibox selection. The x axis will always be the time while the y-axis will be the measurement selected.

Graph with one date

Graph with two dates

Now Here is the problem......the x axis keeps extending as I add a date. I don't want the x-axis to extend. I want my chart to be like this (see image below)

Sample Graph

I want a multi-line pot like the one above not like the previous image.

import pandas as pd
import streamlit as st

import plotly.express as px


@st.cache_data
def load_data():
    data = pd.read_csv('2023.csv', usecols=['tower_id', 'pm2_5', 'humidity', 'temp', 'voc', 'pressure', 'date', 'time'])


    return data


data = load_data()



tower_ids = data['tower_id'].unique()

selected_measurement = st.selectbox('Select Measurement', ('pm2_5', 'humidity', 'temp', 'voc', 'pressure'))

selected_tower = st.selectbox('Which Tower would you want to select?', tower_ids)

dates = data['date'].unique()

filtered_data = data[(data['tower_id'] == selected_tower)]

selected_dates = st.multiselect('Select date(s)', dates)

data['time'] = data['time'].astype(str)

filtered_data = filtered_data[filtered_data['date'].isin(selected_dates)]

fig = px.line(filtered_data, x='time', y=selected_measurement, title="Title", color='date')

st.plotly_chart(fig, use_container_width=True)

I tried to make the date into a string but that did not work.

jared
  • 4,165
  • 1
  • 8
  • 31
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Jul 06 '23 at 19:21

1 Answers1

0

This minimized representation of your problem works as you want it.

import plotly.express as px
import pandas as pd

df  = pd.DataFrame({'time':[1,2,3,3,4,5,1,2].astype(, 'date':[1,1,1,2,2,2,2,2], 'y':[1,1,1,2,2,2,2,2]})

px.line(df, x='time', y='y', color='date')

Here is the situation as you probably have it:

import plotly.express as px
import pandas as pd

df  = pd.DataFrame({'t':[1,2,3,3.1,4.1,5.1,1.1,2.1], 'd':[1,1,1,2,2,2,2,2], 'y':[1,1,1,2,2,2,2,2]})
df['t'] = df['t'].astype(str)

px.line(df, x='t', y='y', color='d')

Notice that I slightly changed the actual "timestamps" of the second "Day" by 0.1 to make them not fully identical to the first set, which is likely the case in your timestamps as well, being off by a few seconds. Because you changed them to String, these became unique categories without natural order. Every unique category gets its own X-tick.

You need to convert your "Time" column to DateTime or Timestamp instead of string.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

StephanT
  • 649
  • 5
  • 12