For school I have to do an assignment where I show the traffic length for measured times for every city. When I try to do this I get this result:
Here's a photo of my dataframe:
Here's my code:
import plotly.express as px
datum = data.loc[data['Date'] == '2020-03-03']
datum = datum.sort_values(by='Time')
dataframe = pd.DataFrame(datum, columns = ['City','Time', 'Date','Traffic_length', 'Traffic_jams'])
dataframe = dataframe.reset_index()
fig = px.line(dataframe, x="Time", y="Traffic_length", color='City')
fig.show()
I want it to show every city with a different line, like this:
I tried multiple things but nothing helped. Does anyone know how to do this with plotly.express?
The problem is that the x
axis is categorical. You can fix it by transforming it to datetime
. The complete code would look like:
import plotly.express as px
datum = data.loc[data['Date'] == '2020-03-03']
datum = datum.sort_values(by='Time')
dataframe = pd.DataFrame(datum, columns = ['City','Time', 'Date','Traffic_length', 'Traffic_jams'])
df = dataframe.reset_index()
df["Time"] = pd.to_datetime(df["Time"]) # <-- This is what you missed
fig = px.line(df, x="Time", y="Traffic_length", color='City')
fig.show()