Skip to content

Commit 02e4d60

Browse files
committed
first pass on time-series notebook
1 parent 4c5740b commit 02e4d60

File tree

1 file changed

+44
-117
lines changed

1 file changed

+44
-117
lines changed

notebooks/time-series.md

Lines changed: 44 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -25,161 +25,107 @@ jupyter:
2525
title: Time Series Plots | plotly
2626
---
2727

28-
#### New to Plotly?
29-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
30-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
31-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
32-
28+
```python
29+
from _plotly_future_ import v4
30+
```
3331

3432

3533
### Time Series Plot with `datetime` Objects ###
3634

35+
Time series can be represented using either `plotly_express` functions (`px.line`, `px.scatter`) or `plotly.graph_objs` charts objects (`go.Scatter`). For more examples of such charts, see the documentation of [line and scatter plots](https://plot.ly/python/line-and-scatter/).
36+
3737
```python
38-
import plotly.plotly as py
39-
import plotly.graph_objs as go
38+
# Using plotly_express
39+
import plotly_express as px
4040

4141
import pandas as pd
42-
from datetime import datetime
43-
4442
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
4543

46-
data = [go.Scatter(x=df.Date, y=df['AAPL.High'])]
47-
48-
py.iplot(data, filename = 'time-series-simple')
44+
fig = px.line(df, x='Date', y='AAPL.High')
45+
fig.show()
4946
```
5047

51-
### Date Strings
52-
5348
```python
54-
import plotly.plotly as py
49+
# Using graph_objs
5550
import plotly.graph_objs as go
5651

5752
import pandas as pd
53+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
5854

59-
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
60-
61-
data = [go.Scatter(
62-
x=df.Date,
63-
y=df['AAPL.Close'])]
64-
65-
py.iplot(data)
55+
fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
56+
fig.show()
6657
```
6758

6859
### Time Series Plot with Custom Date Range
6960

61+
The data range can be set manually using either `datetime.datetime` objects, or date strings.
62+
7063
```python
71-
import plotly.plotly as py
7264
import plotly.graph_objs as go
73-
7465
import datetime
7566

76-
def to_unix_time(dt):
77-
epoch = datetime.datetime.utcfromtimestamp(0)
78-
return (dt - epoch).total_seconds() * 1000
79-
80-
x = [datetime.datetime(year=2013, month=10, day=04),
81-
datetime.datetime(year=2013, month=11, day=05),
82-
datetime.datetime(year=2013, month=12, day=06)]
83-
data = [go.Scatter(
84-
x=x,
85-
y=[1, 3, 6])]
67+
x = [datetime.datetime(year=2013, month=10, day=4),
68+
datetime.datetime(year=2013, month=11, day=5),
69+
datetime.datetime(year=2013, month=12, day=6)]
70+
data = [go.Scatter(x=x, y=[1, 3, 6])]
8671

87-
layout = go.Layout(xaxis = dict(
88-
range = [to_unix_time(datetime.datetime(2013, 10, 17)),
89-
to_unix_time(datetime.datetime(2013, 11, 20))]
90-
))
91-
92-
fig = go.Figure(data = data, layout = layout)
93-
py.iplot(fig)
72+
fig = go.Figure(data=data)
73+
# Use datetime objects to set xaxis range
74+
fig.update(layout_xaxis_range=[datetime.datetime(2013, 10, 17),
75+
datetime.datetime(2013, 11, 20)])
9476
```
9577

96-
### Manually Set Range
97-
9878
```python
99-
import plotly.plotly as py
10079
import plotly.graph_objs as go
101-
10280
import pandas as pd
10381

10482
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
10583

10684
trace_high = go.Scatter(
10785
x=df.Date,
10886
y=df['AAPL.High'],
109-
name = "AAPL High",
110-
line = dict(color = '#17BECF'),
111-
opacity = 0.8)
87+
name="AAPL High",
88+
line_color='deepskyblue',
89+
opacity=0.8)
11290

11391
trace_low = go.Scatter(
11492
x=df.Date,
11593
y=df['AAPL.Low'],
116-
name = "AAPL Low",
117-
line = dict(color = '#7F7F7F'),
118-
opacity = 0.8)
119-
120-
data = [trace_high,trace_low]
121-
122-
layout = dict(
123-
title = "Manually Set Date Range",
124-
xaxis = dict(
125-
range = ['2016-07-01','2016-12-31'])
126-
)
127-
128-
fig = dict(data=data, layout=layout)
129-
py.iplot(fig, filename = "Manually Set Range")
94+
name="AAPL Low",
95+
line_color='dimgray',
96+
opacity=0.8)
97+
98+
fig = go.Figure(data=[trace_high, trace_low])
99+
# Use date string to set xaxis range
100+
fig.update(layout_xaxis_range=['2016-07-01','2016-12-31'],
101+
layout_title_text="Manually Set Date Range")
102+
fig.show()
130103
```
131104

132105
### Time Series With Rangeslider
133106

134107
```python
135-
import plotly.plotly as py
136108
import plotly.graph_objs as go
137-
138109
import pandas as pd
139110

140111
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
141112

142113
trace_high = go.Scatter(
143114
x=df.Date,
144115
y=df['AAPL.High'],
145-
name = "AAPL High",
146-
line = dict(color = '#17BECF'),
147-
opacity = 0.8)
116+
name="AAPL High",
117+
line_color='deepskyblue')
148118

149119
trace_low = go.Scatter(
150120
x=df.Date,
151121
y=df['AAPL.Low'],
152-
name = "AAPL Low",
153-
line = dict(color = '#7F7F7F'),
154-
opacity = 0.8)
155-
156-
data = [trace_high,trace_low]
157-
158-
layout = dict(
159-
title='Time Series with Rangeslider',
160-
xaxis=dict(
161-
rangeselector=dict(
162-
buttons=list([
163-
dict(count=1,
164-
label='1m',
165-
step='month',
166-
stepmode='backward'),
167-
dict(count=6,
168-
label='6m',
169-
step='month',
170-
stepmode='backward'),
171-
dict(step='all')
172-
])
173-
),
174-
rangeslider=dict(
175-
visible = True
176-
),
177-
type='date'
178-
)
179-
)
180-
181-
fig = dict(data=data, layout=layout)
182-
py.iplot(fig, filename = "Time Series with Rangeslider")
122+
name="AAPL Low",
123+
line_color='dimgray')
124+
125+
fig = go.Figure(data=[trace_high, trace_low])
126+
fig.update(layout_title_text='Time Series with Rangeslider',
127+
layout_xaxis_rangeslider_visible=True)
128+
fig.show()
183129
```
184130

185131
### Dash Example
@@ -200,22 +146,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-timeseriesplot/code", wid
200146
#### Reference
201147
See https://plot.ly/python/reference/#layout-xaxis-rangeslider and<br> https://plot.ly/python/reference/#layout-xaxis-rangeselector for more information and chart attribute options!
202148

203-
```python
204-
from IPython.display import display, HTML
205-
206-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
207-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
208-
209-
!pip install git+https://github.com/plotly/publisher.git --upgrade
210-
211-
import publisher
212-
publisher.publish(
213-
'time-series.ipynb', 'python/time-series/', 'Python Time Series | Examples | Plotly',
214-
'How to plot date and time in python. ',
215-
title= 'Time Series Plots | plotly',
216-
name = 'Time Series',
217-
has_thumbnail='true', thumbnail='thumbnail/time-series.jpg',
218-
language='python', page_type='example_index',
219-
display_as='financial', order=0,
220-
ipynb='~notebook_demo/213')
221-
```

0 commit comments

Comments
 (0)