文本中介紹的是如何利用plotly來操作表格,使用的go.Table方法
go.Tableprovides a Table object for detailed data viewing. The data are arranged in 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.
主要的內(nèi)容包含:

image
參考鏈接
https://plotly.com/python/figure-factory-table/
https://plotly.com/python/table/
plotly生成表格
通過自己給定的數(shù)據(jù)來生成表格:go.Table()
number = np.random.randint(80,100,4)
# print(number)
fig = go.Figure(data=[go.Table(
header=dict(values=['A Scores', 'B Scores']), # 設(shè)置表頭
cells=dict(values=[number, # 通過numpy給定數(shù)據(jù)
[95, 85, 75, 95]]))
]
)
fig.show()

image
自定義表格樣式
fig = go.Figure(data=[go.Table(
header=dict(values=['A Scores', 'B Scores'],
line_color='darkslategray',
fill_color='lightskyblue',
align='left'),
cells=dict(values=[[100, 90, 80, 90], # 1st column
[95, 85, 75, 95]], # 2nd column
line_color='darkslategray',
fill_color='lightcyan',
align='center'))
])
?
fig.update_layout(width=600, height=300)
fig.show()

image
使用pandas 生成表格

image-20200706150503434

image-20200706150600178
圖形工廠figure factory
給定數(shù)據(jù)創(chuàng)建
import plotly.figure_factory as ff
data_matrix = [['Country', 'Year', 'Population'], # 每行數(shù)據(jù)記錄
['United States', 2000, 282200000],
['Canada', 2000, 27790000],
['United States', 2005, 295500000],
['Canada', 2005, 32310000],
['United States', 2010, 309000000],
['Canada', 2010, 34000000]]
fig = ff.create_table(data_matrix)
fig.show()

image
添加鏈接 和使用Latex公式
data_matrix = [['User', 'Language', 'Chart Type', '# of Views', 'Equation'], # 第一行數(shù)據(jù)
['<a ,
'<a ,
'<a >Network Graph</a>',
298,'$a^{2}+b^{2}=c^{2}$'], # 第2行數(shù)據(jù)
['<a ,
'<a ,
'<a ,
356,'$F-E+V=2$'] # 第3行數(shù)據(jù)
]
fig = ff.create_table(data_matrix)
fig.show()

image
使用pandas生成多個(gè)表格
data = px.data.gapminder()
df = data[20:25]
?
colorscale = [[0, '#1d004c'],[0.5, '#7d104c'], [1, '#ffffff']]
font=['#FCFCFC', '#00EE00', '#008B00', '#004F00', '#FF3030']
?
fig = ff.create_table(df, # 直接通過pandas創(chuàng)建
height_constant=50, # 每行記錄的寬度
colorscale=colorscale, # 顏色范圍
font_colors=font # 底部顏色
)
fig.show()
fig = ff.create_table(df,height_constant=20) # 直接通過pandas創(chuàng)建
fig.show()
縮小間隔

image
Table and Graph
demo(水平方向)
水平方向是根據(jù)相同的x軸來繪制的
# 給定表格數(shù)據(jù)
table_data = [['Team', 'Wins', 'Losses', 'Ties'], # 直接給定每行的數(shù)據(jù)
['Montréal<br>Canadiens', 18, 4, 0], # 數(shù)據(jù)的換行顯示
['Dallas Stars', 18, 5, 0],
['NY Rangers', 16, 5, 0],
['Boston<br>Bruins', 13, 8, 0],
['Chicago<br>Blackhawks', 13, 8, 0],
['LA Kings', 13, 8, 0],
['Ottawa<br>Senators', 12, 5, 0]]
# Initialize a figure with ff.create_table(table_data)
fig = ff.create_table(table_data, # 表格數(shù)據(jù)
height_constant=60) # 間隔
# 給定畫圖的數(shù)據(jù)
x = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers', # 隊(duì)名
'Boston Bruins', 'Chicago Blackhawks', 'LA Kings', 'Ottawa Senators']
GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 2.45, 3.18] # 2種得分
GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.14, 2.77]
# Make traces for graph
fig.add_trace(go.Scatter(x=x, y=GFPG,
marker=dict(color='#0099ff'), # 指定顏色
name='Goals For<br>Per Game', # 名稱
xaxis='x2', yaxis='y2'))
fig.add_trace(go.Scatter(x=x, y=GAPG,
marker=dict(color='#404040'),
name='Goals Against<br>Per Game',
xaxis='x2', yaxis='y2'))
fig.update_layout(
title_text = 'Title of Figure', # 整個(gè)figure的名稱
margin = {'t':50, 'b':100}, # 與頂部和底部的距離
xaxis = {'domain': [0, .5]}, # 表格的x軸范圍
xaxis2 = {'domain': [0.6, 1]}, # 圖形占據(jù)的x軸范圍
yaxis2 = {'anchor': 'x2', # 表示yaxis的繪圖是以x2為基準(zhǔn),title顯示在yaxis上
'title': 'Goals'}
)
fig.show()

image
demo(豎直方向)
豎直方向是根據(jù)y軸來確定的
# Add table data
table_data = [['Team', 'Wins', 'Losses', 'Ties'],
['Montréal<br>Canadiens', 18, 4, 0],
['Dallas Stars', 18, 5, 0],
['NY Rangers', 16, 5, 0],
['Boston<br>Bruins', 13, 8, 0],
['Chicago<br>Blackhawks', 13, 8, 0],
['Ottawa<br>Senators', 12, 5, 0]]
fig = ff.create_table(table_data, height_constant=60)
# Add graph data
teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',
'Boston Bruins', 'Chicago Blackhawks', 'Ottawa Senators']
GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 3.18]
GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.77]
fig.add_trace(go.Bar(x=teams, y=GFPG, xaxis='x2', yaxis='y2', # 上面的表格屬于x1,y1
marker=dict(color='#0099ff'),
name='Goals For<br>Per Game'))
fig.add_trace(go.Bar(x=teams, y=GAPG, xaxis='x2', yaxis='y2',
marker=dict(color='#404040'),
name='Goals Against<br>Per Game'))
fig.update_layout(
title_text = '2016 Hockey Stats',
height = 1000,
margin = {'t':75, # 與頂部的距離
'l':50}, # 與左邊的距離
yaxis = {'domain': [0, 0.5]}, # 從最下面開始,向上為正,y軸的區(qū)間范圍
xaxis2 = {'anchor': 'y2'}, # anchor
yaxis2 = {'domain': [.6, 1], 'anchor': 'x2', 'title': 'Goals'}
)
fig.show()

image
自身數(shù)據(jù)
df = pd.DataFrame({"name":["xiaoming","xiaohong","zhangshan","lisi"],
"age":np.random.randint(22,30,4),
"address":["shenzhen","guangzhou","changsha","shanghai"],
"chinese":np.random.randint(80,100,4),
"math":np.random.randint(80,100,4)})
colorscale = [[0, '#1d004c'],[0.5, '#7d104c'], [1, '#ffffff']]
font=['#FCFCFC', '#00EE00', '#FF3030']
fig = ff.create_table(df, # 直接通過pandas創(chuàng)建
height_constant=50, # 每行記錄的寬度
colorscale=colorscale, # 顏色范圍
font_colors=font # 底部顏色
)
# fig.show()
fig.add_trace(go.Bar(x=df["name"].tolist(),
y=df["chinese"].tolist(),
xaxis='x2', yaxis='y2',
marker=dict(color='#580bd3'),
name='chinese')
)
fig.add_trace(go.Bar(x=df["name"].tolist(),
y=df["math"].tolist(),
xaxis='x2', yaxis='y2',
marker=dict(color='#0099ff'),
name='math')
)
fig.update_layout(
title_text = 'how to use factory figure',
height = 800,
margin = {'t':75, # 與頂部的距離
'l':50}, # 與左邊的距離
yaxis = {'domain': [0, 0.5]}, # 從最下面開始,向上為正,y軸的區(qū)間范圍
xaxis2 = {'anchor': 'y2'}, # anchor
yaxis2 = {'domain': [.6, 1], 'anchor': 'x2', 'title': 'Goals'}
)

image