1.添加自定义样式的表格
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:表示单元格的背景颜色。
celloc:表示单元格文本的对齐方式,支持' left'、'right '和'center' 三种取值,默认值为'right'
colWidths :表示每列的宽度。
rowLabels:表示行标题的文本。
rowColours; 表示行标题所在单元格的背景颜色。
rowLoc:表示行标题的对齐方式。
colLabels:表示列标题的文本。
colColours :表示列标题所在单元格的背景颜色。
coLoc:表示列标题的对齐方式。
loc :表示表格与绘图区域的对齐方式。
2.实例:果酱面包配料比例
具体代码如下:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 绘制饼图
kinds = ['面粉', '全麦粉', '酵母', '苹果酱', '鸡蛋', '黄油', '盐', '白糖']
weight = [250, 150, 4, 250, 50, 30, 4, 20]
plt.pie(weight, autopct='%3.1f%%')
# 添加图例(通过bbox_to_anchor精细调整图例位置)
plt.legend(kinds, loc='upper right', bbox_to_anchor=[2.2, 0.9], ncol=4)
# bbox to anchor=[2.2, 0.8]表示在图形的外面去添加图例,ncol=4 表示将图例弄成4列
cellColors =[['green']*8]#表示将8个单元格的背景颜色填充为绿色
# 添加表格(通过bbox精细调整表格位置和大小)bbox =[xmin, ymin, width, height]
plt.table(cellText=[weight], cellLoc='center', rowLabels=['重量(g)'], colLabels=kinds, loc='bottom'
, bbox=[1.3, 0.4, 0.9, 0.2],rowColours=['pink'],cellColours=cellColors,colColours= ['green']*8)
plt.show()
运行效果如下: