0

I would like a specific cell in a table to be 'center' aligned.

I used cell._text.set_horizontalalignment('center') and cell._text.set_verticalalignment('center'), but it is not making any changes in the cell.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

gs = gridspec.GridSpec(4, 2)
subplot=plt.subplot(gs[0, 0]) 

tab = plt.table(cellText=a,bbox = [-0.15,0,1.25,1])
subplot.axis('off')

for key, cell in tab.get_celld().items():
    row, col = key
    if row==1 and col==1:
        cell._text.set_weight('bold')
        cell._text.set_horizontalalignment('center') 
        cell._text.set_verticalalignment('center')
plt.show()

cell._text.set_weight('bold') sets the text in Bold weight. But text alignment is not working.

1 Answers1

1

Adding the below codes to my script does the trick. Thank you

cells = tab.properties()["celld"]
cells[1, 1]._loc = 'center'