如何在jupyter中绘制时间序列图?

问题描述 投票:1回答:1

我已尝试绘制数据以实现something like this

enter image description here

但是我不能,我只是通过密谋达到了this graph

enter image description here

Here is the small sample of my data

有人知道如何获得该图吗?

提前感谢

python-3.x pandas jupyter-notebook plotly data-analysis
1个回答
0
投票

欢迎来到论坛。

您会在plotly.ly/python的时间序列中找到很多好东西。尽管如此,我还是想分享一些我认为非常有用的实用细节:

  1. 在熊猫数据框中组织数据
  2. 使用fig=go.Figure([go.Scatter()])设置基本的绘图结构
  3. 使用fig.add_traces([go.Scatter()])在该结构中添加所需的添加物>
  4. 图:

enter image description here

代码:

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=rows*10)#.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()
© www.soinside.com 2019 - 2024. All rights reserved.