How correct added to plotly two csv files visualization

Viewed 20

I have two csv files with data. I can put them on one graph, but after hovering over the graph, only the data from the first file has a legend. The second data is in the form of a dot and when you hover over the dot, you do not see what data it is. How to add it?

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash


df = pd.read_csv('/file1.csv')
df.head()

df2 = pd.read_csv('file2.csv')
df.head()

fig = px.line(df, x = 'data', y ='cena', title='bitek')
fig.add_scatter(x=df2['data'], y =df2['prediction'])

fig.update_traces(mode='markers+lines')

fig.show()

Foto visualizations

1 Answers
  • have simulated 2 CSVs to make your code runnable
  • have named the first trace update_traces(name="cena")
  • have named the second trace fig.add_scatter(x=df2['data'], y =df2['prediction'], name="prediction")
  • have ensured legend is shown for both traces fig.update_traces(mode="markers+lines", showlegend=True)

full code

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import io
import dash


# df = pd.read_csv('/file1.csv')
df = pd.read_csv(
    io.StringIO(
        "data,cena\n2022-09-07 15:00:00,19229.35\n2022-09-07 16:00:00,19285.9\n2022-09-07 17:00:00,19290.96\n2022-09-07 18:00:00,19232.12\n2022-09-07 19:00:00,19290.08\n2022-09-07 20:00:00,19362.59\n2022-09-07 21:00:00,19246.19\n2022-09-07 22:00:00,19226.15\n2022-09-07 23:00:00,19247.34\n2022-09-08 00:00:00,19205.69\n2022-09-08 01:00:00,19367.06\n2022-09-08 02:00:00,19366.95\n2022-09-08 03:00:00,19262.34\n2022-09-08 04:00:00,19286.01\n2022-09-08 05:00:00,19237.16\n2022-09-08 06:00:00,19256.23\n2022-09-08 07:00:00,19264.8\n2022-09-08 08:00:00,19253.53\n2022-09-08 10:00:00,19246.3\n2022-09-08 11:00:00,19231.44\n2022-09-08 12:00:00,19272.24\n2022-09-08 13:00:00,19267.1\n2022-09-08 15:00:00,19205.26\n2022-09-08 16:00:00,19210.45\n2022-09-08 18:00:00,19350.18\n2022-09-08 19:00:00,19271.07\n2022-09-08 20:00:00,19341.29\n2022-09-08 21:00:00,19259.34\n2022-09-08 22:00:00,19218.19\n2022-09-08 23:00:00,19202.85\n2022-09-09 00:00:00,19295.39\n2022-09-09 01:00:00,19310.71\n2022-09-09 02:00:00,19364.91\n2022-09-09 03:00:00,19201.34\n2022-09-09 05:00:00,19308.7\n2022-09-09 07:00:00,19312.15\n2022-09-09 08:00:00,19383.58\n2022-09-09 09:00:00,19287.5\n2022-09-09 10:00:00,19246.2\n2022-09-09 11:00:00,19207.26\n2022-09-09 12:00:00,19320.59\n"
    )
)
df.head()

# df2 = pd.read_csv('file2.csv')
df2 = pd.read_csv(io.StringIO('data,prediction\n2022-09-07 15:00:00,19311.7\n2022-09-07 17:00:00,19364.37\n2022-09-07 18:00:00,19236.29\n2022-09-07 19:00:00,19287.77\n2022-09-07 21:00:00,19265.57\n2022-09-07 23:00:00,19327.47\n2022-09-08 02:00:00,19397.28\n2022-09-08 03:00:00,19279.43\n2022-09-08 04:00:00,19230.03\n2022-09-08 05:00:00,19281.93\n2022-09-08 06:00:00,19219.53\n2022-09-08 07:00:00,19228.21\n2022-09-08 08:00:00,19241.33\n2022-09-08 09:00:00,19283.4\n2022-09-08 10:00:00,19331.62\n2022-09-08 11:00:00,19351.29\n2022-09-08 13:00:00,19313.87\n2022-09-08 14:00:00,19382.66\n2022-09-08 15:00:00,19382.88\n2022-09-08 20:00:00,19321.32\n2022-09-08 21:00:00,19263.72\n2022-09-08 22:00:00,19368.87\n2022-09-08 23:00:00,19330.11\n2022-09-09 01:00:00,19308.24\n2022-09-09 02:00:00,19291.68\n2022-09-09 03:00:00,19317.36\n2022-09-09 06:00:00,19339.32\n2022-09-09 07:00:00,19257.35\n2022-09-09 08:00:00,19370.0\n2022-09-09 09:00:00,19351.01\n2022-09-09 10:00:00,19344.93\n2022-09-09 12:00:00,19347.61\n'))
df.head()

fig = px.line(df, x="data", y="cena", title="bitek").update_traces(name="cena")
fig.add_scatter(x=df2['data'], y =df2['prediction'], name="prediction")

fig.update_traces(mode="markers+lines", showlegend=True)

fig.show()

output

enter image description here

Related