Matplotlib 表格行标签字体颜色和大小

Dan*_*ty2 3 python matplotlib python-3.x

鉴于下表:

import matplotlib.pyplot as plt
table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], # rows of data values
          rowLabels=['1','2','3','4','5'],
          cellLoc="left",
          rowLoc='left',
          bbox=[0,0,.2,1], # [left,bottom,width,height]
          edges="")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想将数字 (1-5) 的颜色更改为灰色,将字体大小更改为 12 磅。

Ser*_*ity 7

您需要获取单元格的文本字体属性:

import matplotlib.pyplot as plt
table=plt.table(cellText=[' ', ' ', ' ', ' ', ' '], #rows of data values
          rowLabels=['1','2','3','4','5'],
          cellLoc="left",
          rowLoc='left',
          bbox=[0,0,.2,1],#[left,bottom,width,height]
          edges="")

# iterate through cells of a table
table_props = table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: 
        cell.get_text().set_fontsize(20)
        cell.get_text().set_color('grey')
plt.show()
Run Code Online (Sandbox Code Playgroud)

另一种获取单元格文本属性的方法是使用单元格索引 (i, j):

table[(i, j)].get_text().set_fontsize(12)
table[(i, j)].get_text().set_color('red')
Run Code Online (Sandbox Code Playgroud)

Matplotlib 文本字体属性在这里描述:http ://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

结果,第一个代码绘制了这个图: 在此处输入图片说明