1
1
---
2
2
jupyter :
3
3
jupytext :
4
+ notebook_metadata_filter : all
4
5
text_representation :
5
6
extension : .md
6
7
format_name : markdown
@@ -25,91 +26,74 @@ jupyter:
25
26
title : Tables | plotly
26
27
---
27
28
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: Table traces are available in version <b >2.2.1+</b >.<br >
34
- Run ` pip install plotly --upgrade ` to update your Plotly version
29
+ ` go.Table ` provides a Table object for detailed data viewing. The data are arranged in
30
+ a grid of rows and columns. Most styling can be specified for header, columns, rows or individual cells. Table is using a column-major order, ie. the grid is represented as a vector of column vectors.
35
31
36
- ``` python
37
- import plotly
38
- plotly.__version__
39
- ```
32
+ Note that [ Dash] ( https://dash.plot.ly/ ) provides a different type of [ DataTable] ( https://dash.plot.ly/datatable ) .
40
33
41
34
#### Basic Table
42
35
43
36
``` python
44
- import plotly.plotly as py
45
37
import plotly.graph_objs as go
46
38
47
- trace = go.Table(
48
- header = dict (values = [' A Scores' , ' B Scores' ]),
49
- cells = dict (values = [[100 , 90 , 80 , 90 ],
50
- [95 , 85 , 75 , 95 ]]))
39
+ trace = go.Table(header = dict (values = [' A Scores' , ' B Scores' ]),
40
+ cells = dict (values = [[100 , 90 , 80 , 90 ], [95 , 85 , 75 , 95 ]]))
51
41
52
- data = [trace]
53
- py.iplot(data, filename = ' basic_table ' )
42
+ fig = go.Figure( data = [trace])
43
+ fig.show( )
54
44
```
55
45
56
46
#### Styled Table
57
47
58
48
``` python
59
- import plotly.plotly as py
60
49
import plotly.graph_objs as go
61
50
62
51
trace = go.Table(
63
52
header = dict (values = [' A Scores' , ' B Scores' ],
64
- line = dict (color = ' #7D7F80' ),
65
- fill = dict (color = ' #a1c3d1' ),
66
- align = [' left' ] * 5 ),
67
- cells = dict (values = [[100 , 90 , 80 , 90 ],
68
- [95 , 85 , 75 , 95 ]],
69
- line = dict (color = ' #7D7F80' ),
70
- fill = dict (color = ' #EDFAFF' ),
71
- align = [' left' ] * 5 ))
72
-
73
- layout = dict (width = 500 , height = 300 )
74
- data = [trace]
75
- fig = dict (data = data, layout = layout)
76
- py.iplot(fig, filename = ' styled_table' )
53
+ line_color = ' #7D7F80' ,
54
+ fill_color = ' #a1c3d1' ,
55
+ align = ' left' ),
56
+ cells = dict (values = [[100 , 90 , 80 , 90 ], # 1st column
57
+ [95 , 85 , 75 , 95 ]], # 2nd column
58
+ line_color = ' #7D7F80' ,
59
+ fill_color = ' #EDFAFF' ,
60
+ align = ' left' ))
61
+
62
+ fig = go.Figure(data = [trace])
63
+ fig.update(layout_width = 500 , layout_height = 300 )
77
64
```
78
65
79
- #### Use a Panda's Dataframe
66
+ #### Use a Pandas Dataframe
80
67
81
68
``` python
82
- import plotly.plotly as py
83
69
import plotly.graph_objs as go
84
-
85
70
import pandas as pd
86
71
87
72
df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv' )
88
73
89
74
trace = go.Table(
90
75
header = dict (values = list (df.columns),
91
- fill = dict ( color = ' #C2D4FF' ) ,
92
- align = [ ' left' ] * 5 ),
76
+ fill_color = ' #C2D4FF' ,
77
+ align = ' left' ),
93
78
cells = dict (values = [df.Rank, df.State, df.Postal, df.Population],
94
- fill = dict ( color = ' #F5F8FF' ) ,
95
- align = [ ' left' ] * 5 ))
79
+ fill_color = ' #F5F8FF' ,
80
+ align = ' left' ))
96
81
97
- data = [trace]
98
- py.iplot(data, filename = ' pandas_table ' )
82
+ fig = go.Figure( data = [trace])
83
+ fig.show( )
99
84
```
100
85
101
86
#### Changing Row and Column Size
102
87
103
88
``` python
104
- import plotly.plotly as py
105
89
import plotly.graph_objs as go
106
90
107
- values = [[[ ' Salaries' , ' Office' , ' Merchandise' , ' Legal' , ' <b>TOTAL<br>EXPENSES</b>' ]],
108
- [ [" Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ,
91
+ values = [[' Salaries' , ' Office' , ' Merchandise' , ' Legal' , ' <b>TOTAL<br>EXPENSES</b>' ], # 1st col
92
+ [" Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ,
109
93
" Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ,
110
94
" Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ,
111
95
" Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ,
112
- " Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ]]]
96
+ " Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad" ]]
113
97
114
98
115
99
trace0 = go.Table(
@@ -118,138 +102,115 @@ trace0 = go.Table(
118
102
header = dict (
119
103
values = [[' <b>EXPENSES</b><br>as of July 2017' ],
120
104
[' <b>DESCRIPTION</b>' ]],
121
- line = dict ( color = ' #506784' ) ,
122
- fill = dict ( color = ' #119DFF' ) ,
123
- align = [' left' ,' center' ],
124
- font = dict (color = ' white' , size = 12 ),
125
- height = 40
105
+ line_color = ' #506784' ,
106
+ fill_color = ' #119DFF' ,
107
+ align = [' left' ,' center' ],
108
+ font = dict (color = ' white' , size = 12 ),
109
+ height = 40
126
110
),
127
111
cells = dict (
128
- values = values,
129
- line = dict ( color = ' #506784' ) ,
130
- fill = dict (color = [' #25FEFD' , ' white' ]),
131
- align = [' left' , ' center' ],
132
- font = dict (color = ' #506784' , size = 12 ),
133
- height = 30
112
+ values = values,
113
+ line_color = ' #506784' ,
114
+ fill = dict (color = [' #25FEFD' , ' white' ]),
115
+ align = [' left' , ' center' ],
116
+ font = dict (color = ' #506784' , size = 12 ),
117
+ height = 30
134
118
))
135
119
136
- data = [trace0]
137
-
138
- py.iplot(data, filename = " Row and Column Size" )
120
+ fig = go.Figure(data = [trace0])
121
+ fig.show()
139
122
```
140
123
141
124
#### Alternating Row Colors
142
125
143
126
``` python
144
- import plotly.plotly as py
145
127
import plotly.graph_objs as go
146
128
147
129
headerColor = ' grey'
148
130
rowEvenColor = ' lightgrey'
149
131
rowOddColor = ' white'
150
132
151
133
trace0 = go.Table(
152
- header = dict (
153
- values = [[' <b>EXPENSES</b>' ],
154
- [' <b>Q1</b>' ],
155
- [' <b>Q2</b>' ],
156
- [' <b>Q3</b>' ],
157
- [' <b>Q4</b>' ]],
158
- line = dict (color = ' #506784' ),
159
- fill = dict (color = headerColor),
160
- align = [' left' ,' center' ],
161
- font = dict (color = ' white' , size = 12 )
134
+ header = dict (
135
+ values = [' <b>EXPENSES</b>' ,' <b>Q1</b>' ,' <b>Q2</b>' ,' <b>Q3</b>' ,' <b>Q4</b>' ],
136
+ line_color = ' #506784' ,
137
+ fill_color = headerColor,
138
+ align = [' left' ,' center' ],
139
+ font = dict (color = ' white' , size = 12 )
162
140
),
163
- cells = dict (
164
- values = [
165
- [[' Salaries' , ' Office' , ' Merchandise' , ' Legal' , ' <b>TOTAL</b>' ]],
166
- [[1200000 , 20000 , 80000 , 2000 , 12120000 ]],
167
- [[1300000 , 20000 , 70000 , 2000 , 130902000 ]],
168
- [[1300000 , 20000 , 120000 , 2000 , 131222000 ]],
169
- [[1400000 , 20000 , 90000 , 2000 , 14102000 ]]],
170
- line = dict (color = ' #506784' ),
171
- fill = dict (color = [rowOddColor,rowEvenColor,rowOddColor, rowEvenColor,rowOddColor]),
141
+ cells = dict (
142
+ values = [
143
+ [' Salaries' , ' Office' , ' Merchandise' , ' Legal' , ' <b>TOTAL</b>' ],
144
+ [1200000 , 20000 , 80000 , 2000 , 12120000 ],
145
+ [1300000 , 20000 , 70000 , 2000 , 130902000 ],
146
+ [1300000 , 20000 , 120000 , 2000 , 131222000 ],
147
+ [1400000 , 20000 , 90000 , 2000 , 14102000 ]],
148
+ line_color = ' #506784' ,
149
+ # 2-D list of colors for alternating rows
150
+ fill_color = [[rowOddColor,rowEvenColor,rowOddColor, rowEvenColor,rowOddColor]* 5 ],
172
151
align = [' left' , ' center' ],
173
152
font = dict (color = ' #506784' , size = 11 )
174
153
))
175
154
176
- data = [trace0]
177
-
178
- py.iplot(data, filename = " alternating row colors" )
155
+ fig = go.Figure(data = [trace0])
156
+ fig.show()
179
157
```
180
158
181
159
#### Row Color Based on Variable
182
160
183
161
``` python
184
- import plotly.plotly as py
185
162
import plotly.graph_objs as go
186
163
187
164
import pandas as pd
188
- import colorlover as cl
189
165
190
- colors = cl.scales[ ' 5 ' ][ ' seq ' ][ ' Blues ' ]
191
- data = { ' Year ' : [ 2010 , 2011 , 2012 , 2013 , 2014 ],
192
- ' Color' : colors}
166
+ colors = [ ' rgb(239, 243, 255) ' , ' rgb(189, 215, 231) ' , ' rgb(107, 174, 214) ' ,
167
+ ' rgb(49, 130, 189) ' , ' rgb(8, 81, 156) ' ]
168
+ data = { ' Year ' : [ 2010 , 2011 , 2012 , 2013 , 2014 ], ' Color' : colors}
193
169
df = pd.DataFrame(data)
194
170
195
-
196
171
trace0 = go.Table(
197
- header = dict (
198
- values = [" Color" , " <b>YEAR</b>" ],
199
- line = dict (color = ' white' ),
200
- fill = dict (color = ' white' ),
201
- align = [' center' ],
202
- font = dict (color = ' black' , size = 12 )
172
+ header = dict (
173
+ values = [" Color" , " <b>YEAR</b>" ],
174
+ line_color = ' white' , fill_color = ' white' ,
175
+ align = ' center' , font = dict (color = ' black' , size = 12 )
203
176
),
204
- cells = dict (
205
- values = [df.Color, df.Year],
206
- line = dict (color = [df.Color]),
207
- fill = dict (color = [df.Color]),
208
- align = ' center' ,
209
- font = dict (color = ' black' , size = 11 )
210
- ))
211
-
212
- data = [trace0]
213
-
214
- py.iplot(data, filename = " row variable color" )
177
+ cells = dict (
178
+ values = [df.Color, df.Year],
179
+ line_color = [df.Color], fill_color = [df.Color],
180
+ align = ' center' , font = dict (color = ' black' , size = 11 )
181
+ ))
182
+ fig = go.Figure(data = [trace0])
183
+ fig.show()
215
184
```
216
185
217
186
#### Cell Color Based on Variable
218
187
219
188
``` python
220
- import plotly.plotly as py
221
189
import plotly.graph_objs as go
222
-
190
+ from plotly.colors import n_colors
223
191
import numpy as np
224
- import colorlover as cl
225
192
226
- colors = cl.scales[ ' 9 ' ][ ' seq ' ][ ' Reds ' ]
193
+ colors = n_colors( ' rgb(255, 200, 200) ' , ' rgb(200, 0, 0) ' , 9 , colortype = ' rgb ' )
227
194
a = np.random.randint(low = 0 , high = 9 , size = 10 )
228
195
b = np.random.randint(low = 0 , high = 9 , size = 10 )
229
196
c = np.random.randint(low = 0 , high = 9 , size = 10 )
230
197
231
198
232
199
trace0 = go.Table(
233
- header = dict (
234
- values = [' <b>Column A</b>' , ' <b>Column B</b>' , ' <b>Column C</b>' ],
235
- line = dict (color = ' white' ),
236
- fill = dict (color = ' white' ),
237
- align = ' center' ,
238
- font = dict (color = ' black' , size = 12 )
200
+ header = dict (
201
+ values = [' <b>Column A</b>' , ' <b>Column B</b>' , ' <b>Column C</b>' ],
202
+ line_color = ' white' , fill_color = ' white' ,
203
+ align = ' center' ,font = dict (color = ' black' , size = 12 )
239
204
),
240
- cells = dict (
241
- values = [a,b,c],
242
- line = dict (color = [np.array(colors)[a],np.array(colors)[b],
243
- np.array(colors)[c]]),
244
- fill = dict (color = [np.array(colors)[a],np.array(colors)[b],
245
- np.array(colors)[c]]),
246
- align = ' center' ,
247
- font = dict (color = ' white' , size = 11 )
205
+ cells = dict (
206
+ values = [a, b, c],
207
+ line_color = [np.array(colors)[a],np.array(colors)[b], np.array(colors)[c]],
208
+ fill_color = [np.array(colors)[a],np.array(colors)[b], np.array(colors)[c]],
209
+ align = ' center' , font = dict (color = ' white' , size = 11 )
248
210
))
249
211
250
- data = [trace0]
251
-
252
- py.iplot(data, filename = " cell variable color" )
212
+ fig = go.Figure(data = [trace0])
213
+ fig.show()
253
214
```
254
215
255
216
### Dash Example
@@ -270,20 +231,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-tableplot/code", width="1
270
231
#### Reference
271
232
For more information on tables and table attributes see: https://plot.ly/python/reference/#table .
272
233
273
- ``` python
274
- from IPython.display import display, HTML
275
-
276
- 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" />' ))
277
- display(HTML(' <link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
278
-
279
- ! pip install git+ https:// github.com/ plotly/ publisher.git -- upgrade
280
- import publisher
281
- publisher.publish(
282
- ' table.ipynb' , ' python/table/' , ' Python Tables | plotly' ,
283
- ' How to make tables in Python with Plotly.' ,
284
- title = ' Tables | plotly' ,
285
- name = ' Tables' ,
286
- thumbnail = ' thumbnail/table.gif' , language = ' python' ,
287
- has_thumbnail = ' true' , display_as = ' basic' , order = 7 ,
288
- ipynb = ' ~notebook_demo/197' )
289
- ```
0 commit comments