Search code examples
pythonplotly

LIne graph using plotly


With this script , it is open 2 dashboards and so 2 graphs How i can modify it to see all on the same graph ? dataframe:



    df = pd.DataFrame( df_abs.to_numpy()[:, 1:].T).reset_index().melt(id_vars="index")
    fig = px.line(df, x="index", y="value", color="variable")
    fig.show()

script dot plotly:

df_abs = pd.DataFrame(df_abs.loc[outlier].iloc[1:]).reset_index().melt(id_vars="index")
fig = px.line(df_abs, x="index", y="value", color="variable")
fig.show()

df = pd.DataFrame( df_abs.to_numpy()[:, 1:].T).reset_index().melt(id_vars="index")
fig = px.line(df, x="index", y="value", color="variable")
fig.show()


Solution

  • You need to use plotly.graph_objects instead of plotly.express.

    plotly.express is great for quick plots but it is somewhat limited compared to plotly.graph_objects.

    Here is a dummy example using 2 dataframes to make it similar to your case.

    import plotly.graph_objects as go
    import plotly.express as px
    
    df1 = px.data.gapminder().query("country in ['Canada']")
    df2 = px.data.gapminder().query("country in ['Italy']")
    
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=df1["lifeExp"], y=df1["gdpPercap"], mode='lines', name='Canada'))
    fig.add_trace(go.Scatter(x=df2["lifeExp"], y=df2["gdpPercap"], mode='lines', name='Italy'))
    fig.show()
    

    enter image description here