text color in matplotlib Table

Hi

I am trying to figure out how to change font color (as opposed to the fill color) in select cells in Table. Is there a way to do this?

Below is an example:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

from matplotlib.table import Table

def main():

data = pd.DataFrame(np.random.random((12,8)),

            columns=['A','B','C','D','E','F','G','H'])

returnsTable(data)

plt.show()

def returnsTable(data, fmt=’{:.2f}/{:.1f}’):

fig=plt.figure(figsize=(8,5))

ax=plt.subplot(111)

ax.set_axis_off()

tb = Table(ax, bbox=[0,0,1,1])

tb.auto_set_font_size(False)

colorDict={-3:"#D00000",-2:"#FF5050",-1:"#FFBFBF",0:"#FFFFFF",1:"#D0FFD0",2:"#40FF40",3:"#00C000"}



nrows, ncols = data.shape

width, height = 1.0 / ncols, 1.0 / nrows



dArray=data.values.reshape(np.product(data.shape))

# mean&sigma..

mean=np.average(dArray)

sigma=np.std(dArray)

# Add cells

for (i,j), val in np.ndenumerate(data):

    z=(val-mean)/sigma

    idx = 0 if int(z)==0 else (max((int(z),-3)) if z<0 else min((int(z),3)))

    color = colorDict[idx]
···

##############################################

IS THERE A WAY TO ALSO CHANGE FONT COLOR?

    tb.add_cell(i+1, j+1, width, height, text=fmt.format(val,z),

                loc='center', facecolor=color)

   

   

# Row labels in cells themselves

# use -1 with edgecolor='none' for outside the grid

for i, label in enumerate(data.index):

    tb.add_cell(i+1, 0, width*2, height, text=label, loc='right', facecolor='none')



# Column Labels...

for j, label in enumerate(data.columns):

    tb.add_cell(0, j+1, width, height/2, text=label, loc='center', facecolor='none')

tb.set_fontsize(8)

ax.add_table(tb)

return fig

main()

This e-Mail and any attachments contain privileged and confidential information of Acadian and may be accessed and read only by the intended recipients.
Any further distribution or reproduction of this material by recipients, or use for any purpose not authorized by Acadian, is strictly prohibited. If you are not the intended recipient and this e-mail and attachments have been sent or passed on to you in error,
please destroy the same and contact us immediately. Confidentiality and privilege are not lost by this transmission having been sent or passed on to you in error. Acadian is not liable for any damage that may be caused by viruses or transmission errors.

Acadian Asset Management LLC is registered as an investment adviser with the U.S. Securities and Exchange Commission. Registered Office: 260 Franklin Street, Boston, Massachusetts 02110. Acadian Asset Management (UK) Limited is a private limited company incorporated
in England, number 05644066, and is authorised and regulated by the Financial Conduct Authority of the United Kingdom. Registered office: 36-38 Cornhill, London, EC3V3ND, United Kingdom. Acadian Asset Management (Singapore) Pte Ltd. (Registration Number: 199902125D)
is a private company limited by shares organized under Singapore law and is authorized by the Monetary Authority of Singapore. Registered office: 8 Shenton Way, #37-02, Singapore 068811.

[AAM_2010_v1.3]


Hmm, it doesn't look like you can do it from the add_cell() function. It
creates a Cell object, which in turn creates a Text object, but provides no
means of passing much down to that Text object. The Table object has a
dictionary of Cell objects: self._cells[(row, col)] = cell, keyed by the
tuple of row and column numbers. You could do something like this:

tb._cells[(i+1, j+1)]._text.set_color('yellow')

Note, I did not test the above line, but I suspect that should work.

Cheers!
Ben Root

···

On Sat, Sep 14, 2013 at 2:45 PM, Alexandre Voitenok < avoitenok@...4449...> wrote:

Hi
I am trying to figure out how to change font color (as opposed to the fill
color) in select cells in Table. Is there a way to do this?
Below is an example:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.table import Table
def main():
    data = pd.DataFrame(np.random.random((12,8)),
                columns=['A','B','C','D','E','F','G','H'])
    returnsTable(data)
    plt.show()

def returnsTable(data, fmt='{:.2f}/{:.1f}'):
    fig=plt.figure(figsize=(8,5))
    ax=plt.subplot(111)
    ax.set_axis_off()
    tb = Table(ax, bbox=[0,0,1,1])
    tb.auto_set_font_size(False)

colorDict={-3:"#D00000",-2:"#FF5050",-1:"#FFBFBF",0:"#FFFFFF",1:"#D0FFD0",2:"#40FF40",3:"#00C000"}

    nrows, ncols = data.shape
    width, height = 1.0 / ncols, 1.0 / nrows

    dArray=data.values.reshape(np.product(data.shape))
    # mean&sigma..
    mean=np.average(dArray)
    sigma=np.std(dArray)

    # Add cells
    for (i,j), val in np.ndenumerate(data):
        z=(val-mean)/sigma
        idx = 0 if int(z)==0 else (max((int(z),-3)) if z<0 else
min((int(z),3)))
        color = colorDict[idx]

##############################################
## IS THERE A WAY TO ALSO CHANGE FONT COLOR?
        tb.add_cell(i+1, j+1, width, height, text=fmt.format(val,z),
                    loc='center', facecolor=color)

    # Row labels in cells themselves
    # use -1 with edgecolor='none' for outside the grid
    for i, label in enumerate(data.index):
        tb.add_cell(i+1, 0, width*2, height, text=label, loc='right',
facecolor='none')

    # Column Labels...
    for j, label in enumerate(data.columns):
        tb.add_cell(0, j+1, width, height/2, text=label, loc='center',
facecolor='none')
    tb.set_fontsize(8)
    ax.add_table(tb)
    return fig

main()