table函数--Matplotlib

函数功能:
Add a table to an Axes.
在坐标系中增加表格

At least one of cellText or cellColours must be specified. These parameters must be 2D lists, in which the outer lists define the rows and the inner list define the column values per row. Each row must have the same number of elements.

参数cellText 或者cellColours 中的一个必须被指定。这些参数必须是二维列表,外层列表定义行数,内层列表定义每一行的列值。每行的元素数量必须保持一致。
The table can optionally have row and column headers, which are configured using rowLabels, rowColours, rowLoc and colLabels, colColours, colLoc respectively.

表格的行、列标题是可选择的,分别使用参数rowLabels, rowColours, rowLoc和参数colLabels, colColours, colLoc设置。

函数语法:

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 : 2D list of str, optional
The texts to place into the table cells.
Note: Line breaks in the strings are currently not accounted for and will result in the text exceeding the cell boundaries.
单元格文本: 可选参数,字符串组成的列表。
放置在表格单元格中的文本
注意:当前不考虑字符串中的换行符,这将导致文本超出单元格边界。

  1. cellText = [[‘A’,‘B’,‘C’,‘D’]] 外层看只有一个元素,因此展示一行,里层列表有四个元素,展示4列。
import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D']]
plt.table(cellText=studentValues)
plt.show()

在这里插入图片描述
2. 当cellText = [[‘A’,‘B’,‘C’,‘D’],[350,150,200,300]] 外层看两个元素,因此展示两行,里层列表有四个元素,展示4列。

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D'],[350,150,200,300]]
plt.table(cellText=studentValues)
plt.show()

在这里插入图片描述
每一行的元素数量必须保持一致,否则报错

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D'],[350,150,200,300],['test','test','test']]
plt.table(cellText=studentValues)
plt.show()

在这里插入图片描述
3. 当cellText = [[‘A’,‘B’,‘C’,‘D’],[350,150,200,300],[‘test’,‘test’,‘test’,‘test’]] 外层看有3个元素,因此展示3行,里层列表有四个元素,展示4列。

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D'],[350,150,200,300],['test','test','test','test']]
plt.table(cellText=studentValues)
plt.show()

在这里插入图片描述
cellColours : 2D list of matplotlib color specs, optional
The background colors of the cells.
单元格颜色: 可选参数,2维颜色列表,单元格的背景颜色;颜色列表必须与参数cellText的列数保持一致
当参数cellColours 与参数cellText的列数不一致

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[[['A','B','C','D'],[350,150,200,300],['test','test','test','test']]]
cellcolors=[['r','y','b','gray']]
plt.table(cellText=studentValues,
          cellColours=cellcolors)
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D'],[350,150,200,300],['test','test','test','test']]
cellcolors=[['r','y','b','gray'],['b','gray','y','r'],['gray','y','b','r']]
plt.table(cellText=studentValues,
          cellColours=cellcolors)
plt.show()

在这里插入图片描述
cellLoc : {‘left’, ‘center’, ‘right’}, default: ‘right’
The alignment of the text within the cells.
单元格位置:单元格中文本的对齐方式,默认右边对齐,可使用**{‘left’, ‘center’, ‘right’}**

  1. 当参数cellLoc ='center’
import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[[350,150,200,300]]
cellcolors=[['r','y','b','gray']]
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc ='center')
plt.show()

在这里插入图片描述

  1. 当参数cellLoc ='left’
import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C','D'],[350,150,200,300],['test','test','test','test']]
cellcolors=[['r','y','b','gray'],['b','gray','y','r'],['gray','y','b','r']]
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc ='left')
plt.show()

在这里插入图片描述
colWidths : list of float, optional
The column widths in units of the axes. If not given, all columns will have a width of 1 / ncols.
列宽:可选参数,浮点型列表,若没有指定列宽,以轴长为单位,所有列的宽度为:1/列数
当列数减少1列,效果如下:整个表格列总宽未变,每一列相对变大;
列宽的设置只影响数据的列宽,对标题文本所在单元格的列宽无影响

import matplotlib.pyplot as plt
import matplotlib as  mpl

mpl.rcParams['font.sans-serif']=['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels=['A难度水平','B难度水平','C难度水平','D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1,0.1,0.1,0.1]
colors = ['r','y','b','gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize':12,
                                 'family':'FangSong',
                                 'fontweight':'bold'},
        explode=explode, colors=colors)

studentValues=[['A','B','C'],[350,150,200],['test','test','test']]
cellcolors=[['r','y','b'],['b','gray','y'],['gray','y','b']]
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc ='left')
plt.show()

在这里插入图片描述
rowLabels : list of str, optional
The text of the row header cells.
行标题标签:可选参数,字符串列表,行标题单元格文本;一维列表,需与参数cellText的外层列表个数(行数)保持一致

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300], ['test', 'test', 'test', 'test']]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r'], ['gray', 'y', 'b', 'r']]
rowLabels = ['aaaaa','bbbbb']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.1] * 4,
          rowLabels=rowLabels)
plt.show()

在这里插入图片描述

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300], ['test', 'test', 'test', 'test']]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r'], ['gray', 'y', 'b', 'r']]
rowLabels = ['aaaaa','bbbbb','ccccc']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.1] * 4,
          rowLabels=rowLabels)
plt.show()

在这里插入图片描述
rowColours : list of matplotlib color specs, optional
The colors of the row header cells.
行标题单元格颜色: 可选参数,指定的颜色列表,行标题单元格颜色。颜色列表为1维列表,且与参数cellText的外层列表个数(行数)保持一致

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300], ['test', 'test', 'test', 'test']]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r'], ['gray', 'y', 'b', 'r']]
rowLabels = ['aaaaa','bbbbb','ccccc']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.1] * 4,
          rowLabels=rowLabels,
          rowColours=['w','c','g'])

plt.show()

在这里插入图片描述

rowLoc : {‘left’, ‘center’, ‘right’}, optional, default: ‘left’
The text alignment of the row header cells.
行标题单元格文本的位置:行标题单元格的文本对齐方式。可选参数,默认:行标题文本左对齐

行标题单元格文本的默认对齐方式看不出来,单元格大小会随着标题文本的长度而缩减,而参数colWidths则对行标题文本所在单元格的宽度无影响,见下图:

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300], ['test', 'test', 'test', 'test']]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r'], ['gray', 'y', 'b', 'r']]
rowLabels = ['a','b','c']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','c','g'],
          rowLoc='left')

plt.show()

在这里插入图片描述
colLabels : list of str, optional
The text of the column header cells.
列标题标签文本: 可选参数,字符串列表,列标题单元格文本。一维数组,且与参数cellText的内层列表个数(列数)保持一致

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels)

plt.show()

在这里插入图片描述
colColours : list of matplotlib color specs, optional
The colors of the column header cells.
列标题单元格颜色,可选参数,颜色列表,一维数组,列标题单元格的颜色。且与参数cellText的内层列表个数(列数)保持一致

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'])

plt.show()

在这里插入图片描述
colLoc : {‘left’, ‘center’, ‘right’}, optional, default: ‘left’
The text alignment of the column header cells.
列标题单元格文本对齐方式:可选参数,默认:左对齐,可取参数**{‘left’, ‘center’, ‘right’}**

位置参数改变,没发现列标题文本位置有变化。

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right')

plt.show()

在这里插入图片描述
loc : str, optional
The position of the cell with respect to ax. This must be one of the codes.
位置,可选参数,字符串格式,单元格相对于坐标系的位置,取值于以下代码:

Location StringLocation Code
‘best’0
‘upper right’1
‘upper left’2
‘lower left’3
‘lower right’4
‘center left’5
‘center right’6
‘lower center’7
‘upper center’8
‘center’9
‘top right’10
‘top left’11
‘bottom left’12
‘bottom right’13
‘right’14
‘left’15
‘top’16
‘bottom’17
  1. 参数loc=bottom
import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right',
          loc='bottom')

plt.show()

在这里插入图片描述
2. 参数loc=top

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right',
          loc='top')

plt.show()

在这里插入图片描述
3. 参数loc=upper right

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right',
          loc='upper right')

plt.show()

在这里插入图片描述
4. 参数loc=upper left

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right',
          loc='upper left')

plt.show()

在这里插入图片描述

bbox : Bbox, optional
A bounding box to draw the table into. If this is not None, this overrides loc.

边框的位置与大小: 可选参数,表格的边界框。若该参数非空,则将覆盖参数loc的设置

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          bbox=(0.8,0.8,0.4,0.3))

plt.show()

在这里插入图片描述
edges : substring of ‘BRTL’ or {‘open’, ‘closed’, ‘horizontal’, ‘vertical’}
The cell edges to be drawn with a line. See also visible_edges.
单元格边缘: **‘BRTL’**字符串(不清楚)或者取值于 {‘open’, ‘closed’, ‘horizontal’, ‘vertical’}
用线条绘制的单元格边缘

参数edges = closed,绘制表格水平、竖直线

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          bbox=(0.8,0.8,0.4,0.3),edges='closed')

plt.show()

在这里插入图片描述

参数edges=‘open’,不绘制表格边框线

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          bbox=(0.8,0.8,0.4,0.3),edges='open')

plt.show()

在这里插入图片描述
参数 edges=‘horizontal’,只绘制表格的水平边框线

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          bbox=(0.8,0.8,0.4,0.3),edges='horizontal')

plt.show()

在这里插入图片描述
参数 edges=‘vertical’,只绘制表格的竖直边框线

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          bbox=(0.8,0.8,0.4,0.3),edges='vertical')

plt.show()

在这里插入图片描述
其他关键字参数:
在这里插入图片描述
设置透明度,字体大小参数没起作用????

import matplotlib as  mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

labels = ['A难度水平', 'B难度水平', 'C难度水平', 'D难度水平']
students = [0.35, 0.15, 0.2, 0.3]
explode = [0.1, 0.1, 0.1, 0.1]
colors = ['r', 'y', 'b', 'gray']

plt.pie(students, autopct='%3.1f%%',
        labels=labels, textprops={'fontsize': 12,
                                  'family': 'FangSong',
                                  'fontweight': 'bold'},
        explode=explode, colors=colors)

studentValues = [['A', 'B', 'C', 'D'], [350, 150, 200, 300]]
cellcolors = [['r', 'y', 'b', 'gray'], ['b', 'gray', 'y', 'r']]
rowLabels = ['a','b']
colLabels=['A_level','B_level','C_level','D_level']
plt.table(cellText=studentValues,
          cellColours=cellcolors,
          cellLoc='center', colWidths=[0.2] * 5,
          rowLabels=rowLabels,
          rowColours=['w','g'],
          rowLoc='left',
          colLabels=colLabels,
          colColours=['r','b','w','g'],
          colLoc='right', loc='upper left',
          edges='closed',
          alpha=0.0, fontsize=500)

plt.show()

在这里插入图片描述

官方文档: table函数

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值