当前有效matplotlib
版本为:3.4.1
。
table()
函数的作用是向子图中添加表格。
函数的签名为matplotlib.pyplot.table(cellText=None, cellColours=None, cellLoc='right', colWidths=None, rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center', loc='bottom', bbox=None, edges='closed', **kwargs)
。
函数的参数为:
cellText
:表格单元格文本,字符串中的换行符暂不支持,可能导致文本超出单元格边界。类型为二维字符串列表。可选参数。cellColours
:表格单元格背景色。类型为二维颜色值列表。可选参数。cellLoc
:表格单元格文本的对齐方式。取值范围为{'left', 'center', 'right'}
,默认值为'right'
。可选参数。colWidths
:表格单元格宽度。类型为浮点数列表。默认每个单元格的宽度为子图宽度/ncols
。可选参数。rowLabels
:表格行表头文本。类型为字符串列表。 可选参数。rowColours
:表格行表头背景色。类型为颜色列表。可选参数。rowLoc
:表格行表头文本的对齐方式。取值范围为{'full', 'left', 'right'}
,默认值为'left'
。colLabels
:表格列表头文本。类型为字符串列表。 可选参数。colColours
:表格列表头背景色。类型为颜色列表。可选参数。colLoc
:表格列表头文本的对齐方式。取值范围为{'full', 'left', 'right'}
,默认值为'left'
。loc
:单元格相对于子图的位置。字符串,取值范围为matplotlib.des
之一,matplotlib.des
={'best': 0, 'bottom': 17, 'bottom left': 12, 'bottom right': 13, 'center': 9, 'center left': 5, 'center right': 6, 'left': 15, 'lower center': 7, 'lower left': 3, 'lower right': 4, 'right': 14, 'top': 16, 'top left': 11, 'top right': 10, 'upper center': 8, 'upper left': 2, 'upper right': 1}
。bbox
:绘制表格的边界框。Bbox
对象,如果该参数为不None
,将会覆盖 loc
参数。可选参数。edges
:单元格边线,该属性会影响各类单元格背景色。取值为 'BRTL'
中字符之一或 {'open', 'closed', 'horizontal', 'vertical'}
。可选参数。**kwargs
:matplotlib.table.Table
对象属性。cellText
和cellColours
其中之一必须之定义,这两个参数必须为二维列表, 外层列表定义行,内层列表定义列,每行必须有相同的元素个数。
表格还可以设置行标签和列标签。分别由rowLabels
、rowColours
、 rowLoc
参数和 colLabels
、 colColours
、colLoc
参数控制。
返回值为matplotlib.table.Table
对象。
table()
函数import matplotlib.pyplot Params['font.family'] = 'simhei'
fig, axes = plt.subplots(1, 2)
# 构造数据
data = [[1, 1], [1, 1]]
# 默认表格样式
axes[0].table(data)
# 隐藏x轴刻度,以防遮盖表格
axes[0].set_xticks([])
axes[0].set_title("默认样式")
# 演示表格参数
axes[1].table(cellText=data, cellColours=[['grey', 'grey'], ['grey', 'red']], cellLoc='center', colWidths=[0.1, 0.1],rowLabels=['a', 'b'], rowColours=['blue', 'blue'], rowLoc='center', colLabels=['A', 'B'],colColours=['green', 'green'], colLoc='left', loc='bottom right', bbox=None, edges='closed')
# 隐藏x轴刻度,以防遮盖表格
axes[1].set_xticks([])
axes[1].set_title("自定义样式")plt.show()
DataFrame
定制参数import matplotlib.pyplot as plt
import pandas Params['font.family'] = 'simhei'data = [[1, 1], [1, 1]]
df = pd.DataFrame(data)plt.table(cellText=df.values, rowLabels=df.index, colLabelslumns)
icks([])
plt.title("使用DataFrame")
plt.show()
本文发布于:2024-02-01 20:33:06,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679078639267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |