Skip to content

Commit 75fca75

Browse files
committed
table notebook
1 parent 27ecd50 commit 75fca75

File tree

1 file changed

+87
-143
lines changed

1 file changed

+87
-143
lines changed

notebooks/table.md

Lines changed: 87 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
jupyter:
33
jupytext:
4+
notebook_metadata_filter: all
45
text_representation:
56
extension: .md
67
format_name: markdown
@@ -25,91 +26,74 @@ jupyter:
2526
title: Tables | plotly
2627
---
2728

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.
3531

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).
4033

4134
#### Basic Table
4235

4336
```python
44-
import plotly.plotly as py
4537
import plotly.graph_objs as go
4638

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]]))
5141

52-
data = [trace]
53-
py.iplot(data, filename = 'basic_table')
42+
fig = go.Figure(data=[trace])
43+
fig.show()
5444
```
5545

5646
#### Styled Table
5747

5848
```python
59-
import plotly.plotly as py
6049
import plotly.graph_objs as go
6150

6251
trace = go.Table(
6352
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)
7764
```
7865

79-
#### Use a Panda's Dataframe
66+
#### Use a Pandas Dataframe
8067

8168
```python
82-
import plotly.plotly as py
8369
import plotly.graph_objs as go
84-
8570
import pandas as pd
8671

8772
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv')
8873

8974
trace = go.Table(
9075
header=dict(values=list(df.columns),
91-
fill = dict(color='#C2D4FF'),
92-
align = ['left'] * 5),
76+
fill_color='#C2D4FF',
77+
align='left'),
9378
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'))
9681

97-
data = [trace]
98-
py.iplot(data, filename = 'pandas_table')
82+
fig = go.Figure(data=[trace])
83+
fig.show()
9984
```
10085

10186
#### Changing Row and Column Size
10287

10388
```python
104-
import plotly.plotly as py
10589
import plotly.graph_objs as go
10690

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",
10993
"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",
11094
"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",
11195
"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"]]
11397

11498

11599
trace0 = go.Table(
@@ -118,138 +102,115 @@ trace0 = go.Table(
118102
header = dict(
119103
values = [['<b>EXPENSES</b><br>as of July 2017'],
120104
['<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
126110
),
127111
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
134118
))
135119

136-
data = [trace0]
137-
138-
py.iplot(data, filename = "Row and Column Size")
120+
fig = go.Figure(data=[trace0])
121+
fig.show()
139122
```
140123

141124
#### Alternating Row Colors
142125

143126
```python
144-
import plotly.plotly as py
145127
import plotly.graph_objs as go
146128

147129
headerColor = 'grey'
148130
rowEvenColor = 'lightgrey'
149131
rowOddColor = 'white'
150132

151133
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)
162140
),
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],
172151
align = ['left', 'center'],
173152
font = dict(color = '#506784', size = 11)
174153
))
175154

176-
data = [trace0]
177-
178-
py.iplot(data, filename = "alternating row colors")
155+
fig = go.Figure(data=[trace0])
156+
fig.show()
179157
```
180158

181159
#### Row Color Based on Variable
182160

183161
```python
184-
import plotly.plotly as py
185162
import plotly.graph_objs as go
186163

187164
import pandas as pd
188-
import colorlover as cl
189165

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}
193169
df = pd.DataFrame(data)
194170

195-
196171
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)
203176
),
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()
215184
```
216185

217186
#### Cell Color Based on Variable
218187

219188
```python
220-
import plotly.plotly as py
221189
import plotly.graph_objs as go
222-
190+
from plotly.colors import n_colors
223191
import numpy as np
224-
import colorlover as cl
225192

226-
colors = cl.scales['9']['seq']['Reds']
193+
colors = n_colors('rgb(255, 200, 200)', 'rgb(200, 0, 0)', 9, colortype='rgb')
227194
a = np.random.randint(low=0, high=9, size=10)
228195
b = np.random.randint(low=0, high=9, size=10)
229196
c = np.random.randint(low=0, high=9, size=10)
230197

231198

232199
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)
239204
),
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)
248210
))
249211

250-
data = [trace0]
251-
252-
py.iplot(data, filename = "cell variable color")
212+
fig = go.Figure(data=[trace0])
213+
fig.show()
253214
```
254215

255216
### Dash Example
@@ -270,20 +231,3 @@ IFrame(src= "https://dash-simple-apps.plotly.host/dash-tableplot/code", width="1
270231
#### Reference
271232
For more information on tables and table attributes see: https://plot.ly/python/reference/#table.
272233

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

Comments
 (0)