简体   繁体   English

Matplotlib 表格行 Label 字体颜色和大小

[英]Matplotlib Table Row Label Font Color and Size

Given the following table:给定下表:

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="")

在此处输入图像描述

I'd like to change the color of the numbers (1-5) to grey and the font size to 12 point.我想将数字 (1-5) 的颜色更改为灰色,将字体大小更改为 12 磅。

You need to get text font properties of the cells:您需要获取单元格的文本字体属性:

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()

Another method to get text properties of the cell is used cell indexes (i, j):另一种获取单元格文本属性的方法是使用单元格索引 (i, j):

table[(i, j)].get_text().set_fontsize(12)
table[(i, j)].get_text().set_color('red')

Matplotlib text font properties are described here: http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties Matplotlib 文本字体属性在此处描述: http ://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

As a result, the first code draw this figure:结果,第一个代码绘制了这个图: 在此处输入图片说明

Inspired by the comments of the question I modified the code to table_cells = table.properties().get('child_artists') or table.properties()['children'] and it worked.受问题评论的启发,我将代码修改为table_cells = table.properties().get('child_artists') or table.properties()['children']并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM