1

I am making a table (inspired by link and link). However how do I set up individual patches? I have a dataframe with patch "values" but I cannot do the following:

tab=ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, loc="center",  cellColours = dfd_color.values, cellHatches=dfd_hatch.values, **kwargs)

My code is below:

def render_mpl_table(data, dfd_color, dfd_hatch,  col_width=0.5, row_height=0.5, font_size=8,
                     header_color='lightgrey', row_colors=['#f1f1f2', 'w'], edge_color='w',
                     bbox=[0, 0, 1, 1], header_columns=0, header_height=2.0,
                     ax=None, **kwargs):

    if ax is None:
        size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
        size[1] += header_height - row_height
        fig, ax = plt.subplots(figsize=size)
        ax.axis('off')

    tab=ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, loc="center",  cellColours = dfd_color.values, **kwargs)
    tab.auto_set_font_size(False)
    tab.set_fontsize(8)
    
    for k, cell in six.iteritems(tab._cells):
        if k[0] == 0 or k[1] < header_columns:
            cell.set_text_props(weight='bold', color='k')
            cell.set_facecolor(header_color)
            cell.set_text_props(rotation='vertical')
            cell.set_height(header_height)
        else:
            cell.set(alpha=0.3)
            cell.set_height(row_height)   
            cell.set(hatch = '/') #works but does not insert individual hatches
    tab.auto_set_column_width(col=list(range(len(dfd['signature']))))        

    
    return ax

render_mpl_table(dfd, dfd_color, dfd_hatch, header_columns=0, header_height=2)
jylls
  • 4,395
  • 2
  • 10
  • 21
anp
  • 11
  • 1

1 Answers1

0

I found a solution inspired by link

plt.rcParams['hatch.color'] = 'red' #does not work in table format. 
cells = tab.properties()["celld"]
cells[0, 0]._loc = 'bottom'
# cells[1,1].set(hatch='/')
for i in range(len(dfd_hatch)):
    for j in range(len(dfd_hatch.columns)):
        pattern=dfd_hatch.iat[i,j]
        cells[i,j].set(hatch=pattern)

One thing that still not works is changing the hatchcolor as this will also change the edgecolor. The solution by setting rcParams['hatch.color']='red' do work in a map I created, but does not work in the table. Either way, I can live with the black hatchcolor.

anp
  • 11
  • 1