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

2024-09-28 20:54:57 发布

您现在位置:Python中文网/ 问答频道 /正文

给出下表:

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

enter image description here

我想把数字(1-5)的颜色改成灰色,把字号改成12号。在


Tags: ofimportdatamatplotlibastablepltleft
1条回答
网友
1楼 · 发布于 2024-09-28 20:54:57

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

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

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

^{pr2}$

Matplotlib文本字体属性如下所述:http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

因此,第一个代码绘制了这个图: enter image description here

相关问题 更多 >