How to show multiple lines instead of one?

D3nic3 :

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:

Result

Here's a photo of my dataframe:

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:

Example

I tried multiple things but nothing helped. Does anyone know how to do this with plotly.express?

villoro :

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()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25531&siteId=1