how to plot 2 or more maesurements(csv) with different time axis into one figure with pandas and plotly

I am doing measurements of a device. I sample different values, like temperatures at different times. Now I like to visualize my data as curves via plotly and pandas.

I am looking for advice how to do this.

To give an example and for simplicity I have 2 data sets stored in 2 seperate csv files, with identical headers. Like: ds1.csv

                     timestamp  value
2021-09-09 16:47:36.231446    355
2021-09-09 16:47:41.208629    355

ds2.csv

                     timestamp  value
2021-09-09 16:47:36.236132     30
2021-09-09 16:47:46.235707     30
2021-09-09 16:47:56.240340     30

The timestamps of the values are not the same, but they are referring to the same clock.

So I like to draw ds1 and ds2 into the same figure with plotly.

fig1 = plotly.express.line(ds1, x = 'timestamp', y = 'value', title='Temp1')
fig2 = plotly.express.line(ds2, x = 'timestamp', y = 'value', title='Temp2')
?fig = fig1 and fig2?
fig.show()

Any idea how to do this?

Answer

Read the files as pandas dataframes. It will be easier to plot two graphs on the same plot if you use plotly graph objects go.Bar or go.Scatter. You can make the xaxis(timestamps) ticks vary linearly by giving a range and dticks parameter.

The below example shows a simple scatter plot which contains two figures plotted on same graph.

import numpy as np
import pandas as pd
import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools

df1 = pd.DataFrame({"X":np.linspace(0,30,10), "Y":np.random.rand(10)})
df2 = pd.DataFrame({"A":np.linspace(0,40,10), "B":np.random.rand(10)})

# plotting the graphs...


def scatter_chart(x, y, color, name):

   trace = go.Scatter(
         x=x.values,
         y=y.values,
         name=name,

         marker=dict(
                color=color,
                line=dict(
                color=color,
                width=1)
              ),
          )
  return trace
# for loop could be used
trace1 = scatter_chart(df2["A"], df2["B"], 'rgb(128, 0, 128)', "df2")
trace2 = scatter_chart(df1["X"], df1["Y"], 'rgba(50, 171, 96, 0.6)', "df1")
fig = tools.make_subplots(rows=1,cols=1, vertical_spacing=0.5)
fig.add_trace(trace1)
fig.add_trace(trace2)

fig.update_layout(
   title="df2 and df1 plot",
   height=600, 
   width=600, 
  # annotations=annotations, 
  xaxis=dict(tickangle=-45),
  legend=dict(x=0.029, y=1.038, font_size=10),
  margin=dict(l=100, r=20, t=70, b=70),
  paper_bgcolor='rgb(248, 248, 255)',
  plot_bgcolor='rgb(248, 248, 255)',)

# xaxis could be customized further
fig['layout']['xaxis'].update(title='', range=[0, 500], dtick=50, autorange=False)
py.iplot(fig, filename='pageviews_plots_4')

Please run the code and see..hope you get the idea.