@@ -8,9 +8,19 @@ jupyter:
8
8
format_version : ' 1.1'
9
9
jupytext_version : 1.1.1
10
10
kernelspec :
11
- display_name : Python 2
11
+ display_name : Python 3
12
12
language : python
13
- name : python2
13
+ name : python3
14
+ language_info :
15
+ codemirror_mode :
16
+ name : ipython
17
+ version : 3
18
+ file_extension : .py
19
+ mimetype : text/x-python
20
+ name : python
21
+ nbconvert_exporter : python
22
+ pygments_lexer : ipython3
23
+ version : 3.6.7
14
24
plotly :
15
25
description : How to create a subplot with tables and charts in Python with Plotly.
16
26
display_as : multiple_axes
@@ -23,26 +33,13 @@ jupyter:
23
33
permalink : python/table-subplots/
24
34
thumbnail : thumbnail/table_subplots.jpg
25
35
title : Table and Chart Subplots | plotly
36
+ v4upgrade : true
26
37
---
27
38
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
- #### Version Check
33
- Note: Tables are available in version <b >2.1.0+</b ><br >
34
- Run ` pip install plotly --upgrade ` to update your Plotly version
35
-
36
- ``` python
37
- import plotly
38
- plotly.__version__
39
- ```
40
-
41
39
#### Import CSV Data
42
40
43
41
``` python
44
- import plotly.plotly as py
45
- import plotly.graph_objs as go
42
+ import plotly.graph_objects as go
46
43
47
44
import pandas as pd
48
45
import re
@@ -55,180 +52,183 @@ for i, row in enumerate(df['Date']):
55
52
datetime = p.split(df[' Date' ][i])[0 ]
56
53
df.iloc[i, 1 ] = datetime
57
54
58
- table = go.Table(
55
+ fig = go.Figure( go.Table(
59
56
columnwidth = [0.4 , 0.47 , 0.48 , 0.4 , 0.4 , 0.45 , 0.5 , 0.6 ],
60
57
header = dict (
61
- # values=list(df.columns[1:]),
62
58
values = [' Date' , ' Number<br>Transactions' , ' Output<br>Volume (BTC)' ,
63
59
' Market<br>Price' , ' Hash<br>Rate' , ' Cost per<br>trans-USD' ,
64
60
' Mining<br>Revenue-USD' , ' Trasaction<br>fees-BTC' ],
65
- font = dict (size = 10 ),
66
- line = dict (color = ' rgb(50, 50, 50)' ),
61
+ font_size = 10 ,
62
+ font_color = ' white' ,
63
+ line_color = ' rgb(50, 50, 50)' ,
67
64
align = ' left' ,
68
- fill = dict ( color = ' #d562be ' ) ,
65
+ fill_color = ' mediumpurple ' ,
69
66
),
70
67
cells = dict (
71
68
values = [df[k].tolist() for k in df.columns[1 :]],
72
- line = dict ( color = ' rgb(50, 50, 50)' ) ,
69
+ line_color = ' rgb(50, 50, 50)' ,
73
70
align = ' left' ,
74
- fill = dict ( color = ' #f5f5fa ' )
71
+ fill_color = ' white '
75
72
)
76
- )
77
- py.iplot([table], filename = ' table-of-mining-data ' )
73
+ ))
74
+ fig.show( )
78
75
```
79
76
80
77
#### Table and Right Aligned Plots
81
78
In Plotly there is no native way to insert a Plotly Table into a Subplot. To do this, create your own ` Layout ` object and defining multiple ` xaxis ` and ` yaxis ` to split up the chart area into different domains. Then for the traces you wish to insert in your final chart, set their ` xaxis ` and ` yaxis ` individually to map to the domains definied in the ` Layout ` . See the example below to see how to align 3 Scatter plots to the right and a Table on the top.
82
79
83
80
``` python
84
- import plotly.plotly as py
85
- import plotly.graph_objs as go
81
+ import plotly.graph_objects as go
86
82
87
83
import numpy as np
88
84
import pandas as pd
89
85
90
- table_trace1 = go.Table(
91
- domain = dict (x = [0 , 0.5 ],
92
- y = [0 , 1.0 ]),
86
+ fig = go.Figure()
87
+
88
+ fig.add_trace(go.Table(
89
+ domain = dict (x = [0 , 0.5 ], y = [0 , 1.0 ]),
93
90
columnwidth = [30 ] + [33 , 35 , 33 ],
94
91
columnorder = [0 , 1 , 2 , 3 , 4 ],
95
92
header = dict (height = 50 ,
96
93
values = [[' <b>Date</b>' ],[' <b>Number<br>transactions</b>' ],
97
94
[' <b>Output<br>volume(BTC)</b>' ], [' <b>Market<br>Price</b>' ]],
98
- line = dict ( color = ' rgb(50, 50, 50)' ) ,
95
+ line_color = ' rgb(50, 50, 50)' ,
99
96
align = [' left' ] * 5 ,
100
97
font = dict (color = [' rgb(45, 45, 45)' ] * 5 , size = 14 ),
101
- fill = dict ( color = ' #d562be' ) ),
98
+ fill_color = ' #d562be' ),
102
99
cells = dict (values = [df[k].tolist() for k in
103
100
[' Date' , ' Number-transactions' , ' Output-volume(BTC)' , ' Market-price' ]],
104
- line = dict ( color = ' #506784' ) ,
101
+ line_color = ' #506784' ,
105
102
align = [' left' ] * 5 ,
106
103
font = dict (color = [' rgb(40, 40, 40)' ] * 5 , size = 12 ),
107
104
format = [None ] + [" , .2f" ] * 2 + [' ,.4f' ],
108
105
prefix = [None ] * 2 + [' $' , u ' \u20BF ' ],
109
106
suffix = [None ] * 4 ,
110
107
height = 27 ,
111
- fill = dict ( color = [' rgb(235, 193, 238)' , ' rgba(228, 222, 249, 0.65)' ]) )
112
- )
108
+ fill_color = [' rgb(235, 193, 238)' , ' rgba(228, 222, 249, 0.65)' ])
109
+ ))
113
110
114
- trace1 = go.Scatter(
111
+ fig.add_trace( go.Scatter(
115
112
x = df[' Date' ],
116
113
y = df[' Hash-rate' ],
117
114
xaxis = ' x1' ,
118
115
yaxis = ' y1' ,
119
116
mode = ' lines' ,
120
117
line = dict (width = 2 , color = ' #9748a1' ),
121
118
name = ' hash-rate-TH/s'
122
- )
119
+ ))
123
120
124
- trace2 = go.Scatter(
121
+ fig.add_trace( go.Scatter(
125
122
x = df[' Date' ],
126
123
y = df[' Mining-revenue-USD' ],
127
124
xaxis = ' x2' ,
128
125
yaxis = ' y2' ,
129
126
mode = ' lines' ,
130
127
line = dict (width = 2 , color = ' #b04553' ),
131
128
name = ' mining revenue'
132
- )
129
+ ))
133
130
134
- trace3 = go.Scatter(
131
+ fig.add_trace( go.Scatter(
135
132
x = df[' Date' ],
136
133
y = df[' Transaction-fees-BTC' ],
137
134
xaxis = ' x3' ,
138
135
yaxis = ' y3' ,
139
136
mode = ' lines' ,
140
137
line = dict (width = 2 , color = ' #af7bbd' ),
141
138
name = ' transact-fee'
142
- )
139
+ ))
143
140
144
141
axis= dict (
145
142
showline = True ,
146
143
zeroline = False ,
147
144
showgrid = True ,
148
145
mirror = True ,
149
146
ticklen = 4 ,
150
- gridcolor = ' #ffffff ' ,
151
- tickfont = dict ( size = 10 )
147
+ gridcolor = ' white ' ,
148
+ tickfont_size = 10
152
149
)
153
150
154
- layout1 = dict (
151
+ fig.update_layout (
155
152
width = 950 ,
156
153
height = 800 ,
157
154
autosize = False ,
158
155
title = ' Bitcoin mining stats for 180 days' ,
159
- margin = dict ( t = 100 ) ,
156
+ margin_t = 100 ,
160
157
showlegend = False ,
161
- xaxis1 = dict (axis, ** dict (domain = [0.55 , 1 ], anchor = ' y1' , showticklabels = False )),
162
- xaxis2 = dict (axis, ** dict (domain = [0.55 , 1 ], anchor = ' y2' , showticklabels = False )),
163
- xaxis3 = dict (axis, ** dict (domain = [0.55 , 1 ], anchor = ' y3' )),
164
- yaxis1 = dict (axis, ** dict (domain = [0.66 , 1.0 ], anchor = ' x1' , hoverformat = ' .2f' )),
165
- yaxis2 = dict (axis, ** dict (domain = [0.3 + 0.03 , 0.63 ], anchor = ' x2' , tickprefix = ' $' , hoverformat = ' .2f' )),
166
- yaxis3 = dict (axis, ** dict (domain = [0.0 , 0.3 ], anchor = ' x3' , tickprefix = u ' \u20BF ' , hoverformat = ' .2f' )),
158
+ xaxis1 = dict (axis, domain = [0.55 , 1 ], anchor = ' y1' , showticklabels = False ),
159
+ xaxis2 = dict (axis, domain = [0.55 , 1 ], anchor = ' y2' , showticklabels = False ),
160
+ xaxis3 = dict (axis, domain = [0.55 , 1 ], anchor = ' y3' ),
161
+ yaxis1 = dict (axis, domain = [0.66 , 1.0 ], anchor = ' x1' , hoverformat = ' .2f' ),
162
+ yaxis2 = dict (axis, domain = [0.3 + 0.03 , 0.63 ], anchor = ' x2' , tickprefix = ' $' ,
163
+ hoverformat = ' .2f' ),
164
+ yaxis3 = dict (axis, domain = [0.0 , 0.3 ], anchor = ' x3' , tickprefix = u ' \u20BF ' ,
165
+ hoverformat = ' .2f' ),
167
166
plot_bgcolor = ' rgba(228, 222, 249, 0.65)'
168
167
)
169
168
170
- fig1 = dict (data = [table_trace1, trace1, trace2, trace3], layout = layout1)
171
- py.iplot(fig1, filename = ' table-right-aligned-plots' )
169
+ fig.show()
172
170
```
173
171
174
172
#### Vertical Table and Graph Subplot
175
173
176
174
``` python
177
- import plotly.plotly as py
178
- import plotly.graph_objs as go
175
+ import plotly.graph_objects as go
176
+
177
+ fig = go.Figure()
179
178
180
- table_trace2 = go.Table(
181
- domain = dict (x = [0 , 1 ],
182
- y = [0.7 , 1.0 ]),
179
+ fig.add_trace(go.Table(
180
+ domain = dict (x = [0 , 1 ], y = [0.7 , 1.0 ]),
183
181
columnwidth = [1 , 2 , 2 , 2 ],
184
182
columnorder = [0 , 1 , 2 , 3 , 4 ],
185
183
header = dict (height = 50 ,
186
- values = [[' <b>Date</b>' ],[' <b>Hash Rate, TH/sec </b>' ],
187
- [' <b>Mining revenue< /b>' ], [' <b>Transaction fees </b>' ]],
188
- line = dict ( color = ' rgb(50, 50, 50)' ) ,
184
+ values = [[' <b>Date</b>' ],[' <b>Number<br>transactions </b>' ],
185
+ [' <b>Output<br>volume(BTC)< /b>' ], [' <b>Market<br>Price </b>' ]],
186
+ line_color = ' rgb(50, 50, 50)' ,
189
187
align = [' left' ] * 5 ,
190
188
font = dict (color = [' rgb(45, 45, 45)' ] * 5 , size = 14 ),
191
- fill = dict (color = ' #d562be' )),
192
- cells = dict (values = [df[k].tolist() for k in [' Date' , ' Hash-rate' , ' Mining-revenue-USD' , ' Transaction-fees-BTC' ]],
193
- line = dict (color = ' #506784' ),
189
+ fill_color = ' #d562be' ),
190
+ cells = dict (values = [df[k].tolist() for k in
191
+ [' Date' , ' Number-transactions' , ' Output-volume(BTC)' , ' Market-price' ]],
192
+ line_color = ' #506784' ,
194
193
align = [' left' ] * 5 ,
195
194
font = dict (color = [' rgb(40, 40, 40)' ] * 5 , size = 12 ),
196
195
format = [None ] + [" , .2f" ] * 2 + [' ,.4f' ],
197
196
prefix = [None ] * 2 + [' $' , u ' \u20BF ' ],
198
197
suffix = [None ] * 4 ,
199
198
height = 27 ,
200
- fill = dict (color = [' rgb(235, 193, 238)' , ' rgba(228, 222, 249, 0.65)' ]))
201
- )
199
+ fill_color = [' rgb(235, 193, 238)' , ' rgba(228, 222, 249, 0.65)' ])
200
+ ))
201
+
202
202
203
- trace4 = go.Scatter(
203
+ fig.add_trace( go.Scatter(
204
204
x = df[' Date' ],
205
205
y = df[' Hash-rate' ],
206
206
xaxis = ' x1' ,
207
207
yaxis = ' y1' ,
208
208
mode = ' lines' ,
209
209
line = dict (width = 2 , color = ' #9748a1' ),
210
210
name = ' hash-rate-TH/s'
211
- )
211
+ ))
212
212
213
- trace5 = go.Scatter(
213
+ fig.add_trace( go.Scatter(
214
214
x = df[' Date' ],
215
215
y = df[' Mining-revenue-USD' ],
216
216
xaxis = ' x2' ,
217
217
yaxis = ' y2' ,
218
218
mode = ' lines' ,
219
219
line = dict (width = 2 , color = ' #b04553' ),
220
220
name = ' mining revenue'
221
- )
221
+ ))
222
222
223
- trace6 = go.Scatter(
223
+ fig.add_trace( go.Scatter(
224
224
x = df[' Date' ],
225
225
y = df[' Transaction-fees-BTC' ],
226
226
xaxis = ' x3' ,
227
227
yaxis = ' y3' ,
228
228
mode = ' lines' ,
229
229
line = dict (width = 2 , color = ' #af7bbd' ),
230
230
name = ' transact-fee'
231
- )
231
+ ))
232
232
233
233
axis= dict (
234
234
showline = True ,
@@ -240,47 +240,28 @@ axis=dict(
240
240
tickfont = dict (size = 10 )
241
241
)
242
242
243
- layout2 = dict (
243
+ fig.update_layout (
244
244
width = 950 ,
245
245
height = 800 ,
246
246
autosize = False ,
247
247
title = ' Bitcoin mining stats for 180 days' ,
248
248
margin = dict (t = 100 ),
249
249
showlegend = False ,
250
- xaxis1 = dict (axis, ** dict (domain = [0 , 1 ], anchor = ' y1' , showticklabels = False )),
251
- xaxis2 = dict (axis, ** dict (domain = [0 , 1 ], anchor = ' y2' , showticklabels = False )),
252
- xaxis3 = dict (axis, ** dict (domain = [0 , 1 ], anchor = ' y3' )),
253
- yaxis1 = dict (axis, ** dict (domain = [2 * 0.21 + 0.02 + 0.02 , 0.68 ], anchor = ' x1' , hoverformat = ' .2f' )),
254
- yaxis2 = dict (axis, ** dict (domain = [0.21 + 0.02 , 2 * 0.21 + 0.02 ], anchor = ' x2' , tickprefix = ' $' , hoverformat = ' .2f' )),
255
- yaxis3 = dict (axis, ** dict (domain = [0.0 , 0.21 ], anchor = ' x3' , tickprefix = u ' \u20BF ' , hoverformat = ' .2f' )),
250
+ xaxis1 = dict (axis, domain = [0 , 1 ], anchor = ' y1' , showticklabels = False ),
251
+ xaxis2 = dict (axis, domain = [0 , 1 ], anchor = ' y2' , showticklabels = False ),
252
+ xaxis3 = dict (axis, domain = [0 , 1 ], anchor = ' y3' ),
253
+ yaxis1 = dict (axis, domain = [2 * 0.21 + 0.02 + 0.02 , 0.68 ], anchor = ' x1' , hoverformat = ' .2f' ),
254
+ yaxis2 = dict (axis, domain = [0.21 + 0.02 , 2 * 0.21 + 0.02 ], anchor = ' x2' , tickprefix = ' $' ,
255
+ hoverformat = ' .2f' ),
256
+ yaxis3 = dict (axis, domain = [0.0 , 0.21 ], anchor = ' x3' , tickprefix = u ' \u20BF ' ,
257
+ hoverformat = ' .2f' ),
256
258
plot_bgcolor = ' rgba(228, 222, 249, 0.65)'
257
259
)
258
260
259
- fig2 = dict (data = [table_trace2, trace4, trace5, trace6], layout = layout2)
260
- py.iplot(fig2, filename = ' vertical-stacked-subplot-tables' )
261
+ fig.show()
261
262
```
262
263
263
264
#### Reference
264
265
See https://plot.ly/python/reference/#table for more information regarding chart attributes! <br >
265
266
For examples of Plotly Tables, see: https://plot.ly/python/table/
266
267
267
- ``` python
268
- from IPython.display import display, HTML
269
-
270
- 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" />' ))
271
- display(HTML(' <link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
272
-
273
- ! pip install git+ https:// github.com/ plotly/ publisher.git -- upgrade
274
- import publisher
275
- publisher.publish(
276
- ' table-subplots.ipynb' , ' python/table-subplots/' , ' Table and Chart Subplots' ,
277
- ' How to create a subplot with tables and charts in Python with Plotly.' ,
278
- title = ' Table and Chart Subplots | plotly' ,
279
- has_thumbnail = ' true' , thumbnail = ' thumbnail/table_subplots.jpg' ,
280
- language = ' python' ,
281
- display_as = ' multiple_axes' , order = 11 )
282
- ```
283
-
284
- ``` python
285
-
286
- ```
0 commit comments