4

I have tried to plot the data in order to achieve something like this:

enter image description here

But I could not and I just achieved this graph with plotly:

enter image description here

Here is the small sample of my data

Does anyone know how to achieve that graph?

Thanks in advance

vestland
  • 55,229
  • 37
  • 187
  • 305
Alisha
  • 61
  • 1
  • 6
  • 1
    What code have you written so far? What went good and what part you don't understand? – gosuto Oct 26 '19 at 10:10
  • 1
    [Stack Overflow Discourages Screenshots](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). It is likely, the question will be downvoted, for containing unnecessary screenshots. By using screenshots, you are discouraging anyone from assisting you. No one wants to retype your stuff, from a screenshot, and screenshots are often, not readable. – Trenton McKinney Oct 26 '19 at 23:43
  • 1
    Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/provide-a-reproducible-copy-of-the-dataframe-with-to-clipboard/52413247#52413247) – Trenton McKinney Oct 26 '19 at 23:44

1 Answers1

4

You'll find a lot of good stuff on timeseries on plotly.ly/python. Still, I'd like to share some practical details that I find very useful:

  1. organize your data in a pandas dataframe
  2. set up a basic plotly structure using fig=go.Figure(go.Scatter())
  3. Make your desired additions to that structure using fig.add_traces(go.Scatter())

Plot:

enter image description here

Code:

import plotly.graph_objects as go
import pandas as pd
import numpy as np

# random data or other data sources
np.random.seed(123)
observations = 200
timestep = np.arange(0, observations/10, 0.1)
dates = pd.date_range('1/1/2020', periods=observations)
val1 = np.sin(timestep)
val2=val1+np.random.uniform(low=-1, high=1, size=observations)#.tolist()

# organize data in a pandas dataframe
df= pd.DataFrame({'Timestep':timestep, 'Date':dates,
                               'Value_1':val1,
                               'Value_2':val2})

# Main plotly figure structure
fig = go.Figure([go.Scatter(x=df['Date'], y=df['Value_2'],
                            marker_color='black',
                            opacity=0.6,
                            name='Value 1')])

# One of many possible additions
fig.add_traces([go.Scatter(x=df['Date'], y=df['Value_1'],
                           marker_color='blue',
                           name='Value 2')])

# plot figure
fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305