Python中的 Matplotlib.axes.Axes.table()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.table()函数
matplotlib 库的 axes 模块中的Axes.table()函数也用于将表格添加到 Axes。
Syntax: Axes.table(ax, 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)
Parameters: This method accept the following parameters that are described below:
- cellText : This parameter contains the texts to place into the table cells.
- cellColours : This parameter is the background colors of the cells.
- cellLoc : This parameter is the alignment of the text within the cells.
- colWidths : This parameter is the column widths in units of the axes.
- rowLabels : This parameter is the text of the row header cells.
- rowColours : This parameter is the colors of the row header cells.
- rowLoc : This parameter is the text alignment of the row header cells.
- colLabels : This parameter is the text of the column header cells.
- colColours : This parameter is the colors of the column header cells.
- colLoc : This parameter is the text alignment of the column
header cells. - Loc : This parameter is the position of the cell with respect to ax.
- bbox : This parameter is the bounding box to draw the table into.
- edges : This parameter is the cell edges to be drawn with a line.
Returns: This returns the following:
- table : This method returns the created table.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.table()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
val1 = ["{:X}".format(i) for i in range(10)]
val2 = ["{:02X}".format(10 * i) for i in range(10)]
val3 = [["" for c in range(10)] for r in range(10)]
fig, ax = plt.subplots()
ax.set_axis_off()
table = ax.table(
cellText = val3,
rowLabels = val2,
colLabels = val1,
rowColours =["palegreen"] * 10,
colColours =["palegreen"] * 10,
cellLoc ='center',
loc ='upper left')
ax.set_title('matplotlib.axes.Axes.table() function Example',
fontweight ="bold")
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
data = [[ 66, 174, 71, 58],
[ 58, 139, 45, 164],
[ 89, 52, 18, 81],
[ 78, 58, 123, 68],
[13, 159, 164, 80]]
val1 = ('Geek1', 'Geek2', 'Geek3', 'Geek4')
val2 = ['Month % d' % x for x in (5, 4, 3, 2, 1)]
val3 = np.arange(0, 2500, 500)
val4 = 1000
val5 = plt.cm.plasma(np.linspace(0, 0.5, len(val2)))
val6 = len(data)
val7 = np.arange(len(val1)) + 0.3
val8 = 0.4
val9 = np.zeros(len(val1))
lista = []
fig, ax = plt.subplots()
for row in range(val6):
ax.bar(val7, data[row], val8, bottom = val9,
color = val5[row])
val9 = val9 + data[row]
lista.append([(x // 50) for x in val9])
the_table = ax.table(cellText = lista,
rowLabels = val2,
rowColours = val5,
colLabels = val1,
loc ='bottom')
plt.subplots_adjust(left = 0.2, bottom = 0.2)
ax.set_xticks([])
ax.set_title('matplotlib.axes.Axes.table() function Example',
fontweight ="bold")
plt.grid()
plt.show()
输出: