On Sat, Sep 14, 2013 at 2:45 PM, Alexandre Voitenok <
avoite...@acadian-asset.com> 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()
>
>
>
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
------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. 
http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to