I am plotting a table where text inside a certain cell should change its color depending on its value. Here and here I found the method table._cells[(i,j)]._text.set_color('color')
which seems very helpfull but unfortunately using it in my code raises the KeyError: (2, 1).
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
val1 = 0.3
val2 = -0.7
solution = val1 - val2
row_labels = ['sum1', 'sum2',
'sol']
if val2 >= 0:
table_vals = [[val1], ['- '+str(val2)], ['= '+str(solution)]]
else:
table_vals = [[val1], ['- ('+str(val2)+')'], ['= '+str(solution)]]
table = ax.table(cellText=table_vals, rowLabels=row_labels,
colWidths=[0.1]*2, bbox = [0.3, 0.1, 0.3, 0.3])
for key, cell in table.get_celld().items():
cell.set_edgecolor('#FFFFFF')
cell.set_facecolor('#636363')
cell._text.set_color('#FFFFFF')
if -0.25 < solution < 0.25:
table._cells[(2, 1)]._text.set_color('#008000') # raises KeyError
elif -0.5 < solution <= -0.25 or 0.25 <= solution < 0.5:
table._cells[(2, 1)]._text.set_color('#FFFF00') # raises KeyError
else:
table._cells[(2, 1)]._text.set_color('#FF0000') # raises KeyError
plt.show()
Can anyone tell me what mistakes I made?