如何在Python中使用 Plotly 创建表格?
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
Plotly 中的表格
表格有助于将数据组织成列和行。表格的使用在所有的交流、研究和数据分析中都很普遍。他们优先考虑快速进入和按比例对信息进行基本比较。它可以使用graph_objects类的Table()方法创建。
Syntax: plotly.graph_objects.Table(arg=None, cells=None, columnorder=None, columnwidth=None, header=None, **kwargs)
Parameters:
arg: dict of properties compatible with this constructor or an instance of plotly.graph_objects.Table
cells: plotly.graph_objects.table.Cells instance or dict with compatible properties
columnorder: Specifies the rendered order of the data columns; for example, a value 2 at position 0 means that column index 0 in the data will be rendered as the third column, as columns have an index base of zero.
columnwidth: The width of columns expressed as a ratio. Columns fill the available width in proportion of their specified column widths.
header: plotly.graph_objects.table.Header instance or dict with compatible properties
例子:
Python3
import plotly.graph_objects as go
fig = go.Figure(data=[go.Table(
header=dict(values=['A', 'B']),
cells=dict(values=[[10, 20, 30, 40],
[40, 20, 10, 50]]))
])
fig.show()
Python3
import plotly.graph_objects as go
color1 = 'lightgreen'
color2 = 'lightblue'
fig = go.Figure(data=[go.Table(
header=dict(values=['A', 'B']),
cells=dict(values=[[10, 20, 30, 40],
[40, 20, 10, 50]],
fill_color=[[color1, color2, color1,
color2, color1]*2],))
])
fig.show()
Python3
import plotly.graph_objects as go
color1 = 'lightgreen'
color2 = 'lightblue'
fig = go.Figure(data=[go.Table(
# Ratio for column width
columnwidth=[1, 5],
header=dict(values=['A', 'B']),
cells=dict(values=[[10, 20, 30, 40],
[40, 20, 10, 50]],
fill_color=[[color1, color2, color1,
color2, color1]*2],))
])
fig.show()
输出:
更改行颜色
在替代行中添加颜色将更容易更有效地理解数据。它将数据彼此区分开来,并且可以在数据格式中轻松地单独查看值。
例子:
Python3
import plotly.graph_objects as go
color1 = 'lightgreen'
color2 = 'lightblue'
fig = go.Figure(data=[go.Table(
header=dict(values=['A', 'B']),
cells=dict(values=[[10, 20, 30, 40],
[40, 20, 10, 50]],
fill_color=[[color1, color2, color1,
color2, color1]*2],))
])
fig.show()
输出:
改变行和列大小
可以使用 columnwidth 参数更改行和列的大小。列的宽度以比率表示。列按其指定列宽的比例填充可用宽度。
例子:
Python3
import plotly.graph_objects as go
color1 = 'lightgreen'
color2 = 'lightblue'
fig = go.Figure(data=[go.Table(
# Ratio for column width
columnwidth=[1, 5],
header=dict(values=['A', 'B']),
cells=dict(values=[[10, 20, 30, 40],
[40, 20, 10, 50]],
fill_color=[[color1, color2, color1,
color2, color1]*2],))
])
fig.show()
输出: