@@ -25,161 +25,107 @@ jupyter:
25
25
title : Time Series Plots | plotly
26
26
---
27
27
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
+ ```
33
31
34
32
35
33
### Time Series Plot with ` datetime ` Objects ###
36
34
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
+
37
37
``` python
38
- import plotly.plotly as py
39
- import plotly.graph_objs as go
38
+ # Using plotly_express
39
+ import plotly_express as px
40
40
41
41
import pandas as pd
42
- from datetime import datetime
43
-
44
42
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
45
43
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()
49
46
```
50
47
51
- ### Date Strings
52
-
53
48
``` python
54
- import plotly.plotly as py
49
+ # Using graph_objs
55
50
import plotly.graph_objs as go
56
51
57
52
import pandas as pd
53
+ df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv' )
58
54
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()
66
57
```
67
58
68
59
### Time Series Plot with Custom Date Range
69
60
61
+ The data range can be set manually using either ` datetime.datetime ` objects, or date strings.
62
+
70
63
``` python
71
- import plotly.plotly as py
72
64
import plotly.graph_objs as go
73
-
74
65
import datetime
75
66
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 ])]
86
71
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 )])
94
76
```
95
77
96
- ### Manually Set Range
97
-
98
78
``` python
99
- import plotly.plotly as py
100
79
import plotly.graph_objs as go
101
-
102
80
import pandas as pd
103
81
104
82
df = pd.read_csv(" https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv" )
105
83
106
84
trace_high = go.Scatter(
107
85
x = df.Date,
108
86
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 )
112
90
113
91
trace_low = go.Scatter(
114
92
x = df.Date,
115
93
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()
130
103
```
131
104
132
105
### Time Series With Rangeslider
133
106
134
107
``` python
135
- import plotly.plotly as py
136
108
import plotly.graph_objs as go
137
-
138
109
import pandas as pd
139
110
140
111
df = pd.read_csv(" https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv" )
141
112
142
113
trace_high = go.Scatter(
143
114
x = df.Date,
144
115
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' )
148
118
149
119
trace_low = go.Scatter(
150
120
x = df.Date,
151
121
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()
183
129
```
184
130
185
131
### Dash Example
@@ -200,22 +146,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-timeseriesplot/code", wid
200
146
#### Reference
201
147
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!
202
148
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