簡體   English   中英

Matplotlib 表格行 Label 字體顏色和大小

[英]Matplotlib Table Row Label Font Color and Size

給定下表:

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

在此處輸入圖像描述

我想將數字 (1-5) 的顏色更改為灰色,將字體大小更改為 12 磅。

您需要獲取單元格的文本字體屬性:

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

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

Matplotlib 文本字體屬性在此處描述: http ://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_fontproperties

結果,第一個代碼繪制了這個圖: 在此處輸入圖片說明

受問題評論的啟發,我將代碼修改為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