Dance Party2
Dance Party2

Reputation: 7536

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

enter image description here

I'd like to change the color of the numbers (1-5) to grey and the font size to 12 point.

Upvotes: 4

Views: 14409

Answers (2)

Youth overturn
Youth overturn

Reputation: 417

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.

Upvotes: 1

Serenity
Serenity

Reputation: 36635

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

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

As a result, the first code draw this figure: enter image description here

Upvotes: 12

Related Questions