Esempio n. 1
0
def test_table_cells():
    fig, ax = plt.subplots()
    table = Table(ax)

    cell = table.add_cell(1, 2, 1, 1)
    assert isinstance(cell, CustomCell)
    assert cell is table[1, 2]

    cell2 = CustomCell((0, 0), 1, 2, visible_edges=None)
    table[2, 1] = cell2
    assert table[2, 1] is cell2

    # make sure getitem support has not broken
    # properties and setp
    table.properties()
    plt.setp(table)
Esempio n. 2
0
def test_table_cells():
    fig, ax = plt.subplots()
    table = Table(ax)

    cell = table.add_cell(1, 2, 1, 1)
    assert isinstance(cell, CustomCell)
    assert cell is table[1, 2]

    cell2 = CustomCell((0, 0), 1, 2, visible_edges=None)
    table[2, 1] = cell2
    assert table[2, 1] is cell2

    # make sure gettitem support has not broken
    # properties and setp
    table.properties()
    plt.setp(table)
Esempio n. 3
0
def table_plot(df, figsize=(12, 9), value_format='text', fontsize=None, 
               positive_color=None, negative_color=None, header_color=None, index_color=None, 
               fill_color=None, positive_fill_color=None, negative_fill_color=None, 
               header_fill_color=None, index_fill_color=None, title=None, decimals=2, **kwargs):
    """
    Plot DataFrame as a table

    Parameters
    ----------
    df : DataFrame
        Data to be plotted as a table
    figsize : tuple, optional
        2-ple of (x, y) dimensions for figures in inches
    value_format : string, optional
        Format of the table values. Options supported are 'text' and 'numeric'.
    fontsize : int, optional
        Font size
    positive_color : string, optional
        Color to print positive values
    negative_color : string, optional
        Color to print negative cells
    header_color : string, optional
        Color to print header values
    index_color : string, optional
        Color to print index values
    fill_color : string, optional
        Color to fill table cells
    positive_fill_color : string, optional
        Color to fill cells with positive values
    negative_fill_color : string, optional
        Color to fill cells with negative values
    header_fill_color : string, optional
        Color to fill header cells
    index_fill_color : string, optional
        Color to fill index cells
    title : string, optional
        Table title
    decimals : int, optional
        If value_format is numeric, the number of decimal places used to round values

    Returns
    -------
    fig : Figure
        Figure containing the table plot
    """
    if not isinstance(df, pd.DataFrame):
        raise TypeError('df should be a DataFrame object')    
    if value_format not in ['text', 'numeric']:
        raise ValueError('%s is not a valid format' % value_format)

    # draw table
    fig, ax = plt.subplots(figsize=figsize)
    ax.set_axis_off()
    tb = Table(ax, bbox=[0, 0, 1, 1])
    num_cols, num_rows = len(df.columns), len(df.index)
    width, height = 1.0 / num_cols, 1.0 / num_rows

    # add table cells
    loc='center'
    for (i, j), val in np.ndenumerate(df):
        value = df.iloc[i, j]
        text_color = 'black' 
        cell_color = 'none'
        if fill_color is not None:
            cell_color = fill_color            
        if isinstance(value, int) or isinstance(value, float):
            if np.isnan(value):
                continue
            if positive_color is not None and value >= 0:
                text_color = positive_color
            if positive_fill_color is not None and value >= 0:
                cell_color = positive_fill_color
            if negative_color is not None and value < 0:
                text_color = negative_color
            if negative_fill_color is not None and value < 0:
                cell_color = negative_fill_color
            if value_format == 'text':
                tb.add_cell(i + 1, j + 1, width, height, text=str(val), loc=loc, facecolor=cell_color)
            elif value_format == 'numeric':
                value_str = str(round(val, decimals))
                tb.add_cell(i + 1, j + 1, width, height, text=value_str, loc=loc, facecolor=cell_color)
            tb._cells[(i+1, j+1)]._text.set_color(text_color)
        elif isinstance(value, pd.tslib.Timestamp):
            tb.add_cell(i + 1, j + 1, width, height, text=val.strftime('%d-%b-%Y'), loc=loc, facecolor=cell_color)
            tb._cells[(i+1, j+1)]._text.set_color(text_color)
        else:
            tb.add_cell(i + 1, j + 1, width, height, text=str(val), loc=loc, facecolor=cell_color)
            tb._cells[(i+1, j+1)]._text.set_color(text_color)
            
    if index_color is None:
        index_color='black'
    if index_fill_color is not None:
        cell_color = index_fill_color 
    else:
        cell_color = 'none'
    # row labels
    for i, label in enumerate(df.index):
        tb.add_cell(i + 1, 0, width, height, text=label, loc=loc, edgecolor='none', facecolor=cell_color)
        tb._cells[(i+1, 0)]._text.set_color(index_color)

    if header_color is None:
        header_color='black'
    if header_fill_color is not None:
        cell_color = header_fill_color
    else:
        cell_color = 'none'
    # column labels
    for j, label in enumerate(df.columns):
        tb.add_cell(0, j + 1, width, height, text=label, loc=loc, edgecolor='none', facecolor=cell_color)
        tb._cells[(i+1, 0)]._text.set_color(header_color)

    # set font size
    if fontsize is not None:
        tb_cells = tb.properties()['child_artists']
        for cell in tb_cells:
            cell.set_fontsize(fontsize)
    
    # add table to figure        
    ax.add_table(tb)
    
    # set title
    if title is not None:
        if fontsize is not None:
            ax.set_title(title, fontsize=fontsize)
        else:
            ax.set_title(title)
            
    plt.tight_layout()
    
    return fig
Esempio n. 4
0
def table_plot(df,
               figsize=(12, 9),
               value_format='text',
               fontsize=None,
               positive_color=None,
               negative_color=None,
               header_color=None,
               index_color=None,
               fill_color=None,
               positive_fill_color=None,
               negative_fill_color=None,
               header_fill_color=None,
               index_fill_color=None,
               title=None,
               decimals=2,
               **kwargs):
    """
    Plot DataFrame as a table

    Parameters
    ----------
    df : DataFrame
        Data to be plotted as a table
    figsize : tuple, optional
        2-ple of (x, y) dimensions for figures in inches
    value_format : string, optional
        Format of the table values. Options supported are 'text' and 'numeric'.
    fontsize : int, optional
        Font size
    positive_color : string, optional
        Color to print positive values
    negative_color : string, optional
        Color to print negative cells
    header_color : string, optional
        Color to print header values
    index_color : string, optional
        Color to print index values
    fill_color : string, optional
        Color to fill table cells
    positive_fill_color : string, optional
        Color to fill cells with positive values
    negative_fill_color : string, optional
        Color to fill cells with negative values
    header_fill_color : string, optional
        Color to fill header cells
    index_fill_color : string, optional
        Color to fill index cells
    title : string, optional
        Table title
    decimals : int, optional
        If value_format is numeric, the number of decimal places used to round values

    Returns
    -------
    fig : Figure
        Figure containing the table plot
    """
    if not isinstance(df, pd.DataFrame):
        raise TypeError('df should be a DataFrame object')
    if value_format not in ['text', 'numeric']:
        raise ValueError('%s is not a valid format' % value_format)

    # draw table
    fig, ax = plt.subplots(figsize=figsize)
    ax.set_axis_off()
    tb = Table(ax, bbox=[0, 0, 1, 1])
    num_cols, num_rows = len(df.columns), len(df.index)
    width, height = 1.0 / num_cols, 1.0 / num_rows

    # add table cells
    loc = 'center'
    for (i, j), val in np.ndenumerate(df):
        value = df.iloc[i, j]
        text_color = 'black'
        cell_color = 'none'
        if fill_color is not None:
            cell_color = fill_color
        if isinstance(value, int) or isinstance(value, float):
            if np.isnan(value):
                continue
            if positive_color is not None and value >= 0:
                text_color = positive_color
            if positive_fill_color is not None and value >= 0:
                cell_color = positive_fill_color
            if negative_color is not None and value < 0:
                text_color = negative_color
            if negative_fill_color is not None and value < 0:
                cell_color = negative_fill_color
            if value_format == 'text':
                tb.add_cell(i + 1,
                            j + 1,
                            width,
                            height,
                            text=str(val),
                            loc=loc,
                            facecolor=cell_color)
            elif value_format == 'numeric':
                value_str = str(round(val, decimals))
                tb.add_cell(i + 1,
                            j + 1,
                            width,
                            height,
                            text=value_str,
                            loc=loc,
                            facecolor=cell_color)
            tb._cells[(i + 1, j + 1)]._text.set_color(text_color)
        elif isinstance(value, pd.tslib.Timestamp):
            tb.add_cell(i + 1,
                        j + 1,
                        width,
                        height,
                        text=val.strftime('%d-%b-%Y'),
                        loc=loc,
                        facecolor=cell_color)
            tb._cells[(i + 1, j + 1)]._text.set_color(text_color)
        else:
            tb.add_cell(i + 1,
                        j + 1,
                        width,
                        height,
                        text=str(val),
                        loc=loc,
                        facecolor=cell_color)
            tb._cells[(i + 1, j + 1)]._text.set_color(text_color)

    if index_color is None:
        index_color = 'black'
    if index_fill_color is not None:
        cell_color = index_fill_color
    else:
        cell_color = 'none'
    # row labels
    for i, label in enumerate(df.index):
        tb.add_cell(i + 1,
                    0,
                    width,
                    height,
                    text=label,
                    loc=loc,
                    edgecolor='none',
                    facecolor=cell_color)
        tb._cells[(i + 1, 0)]._text.set_color(index_color)

    if header_color is None:
        header_color = 'black'
    if header_fill_color is not None:
        cell_color = header_fill_color
    else:
        cell_color = 'none'
    # column labels
    for j, label in enumerate(df.columns):
        tb.add_cell(0,
                    j + 1,
                    width,
                    height,
                    text=label,
                    loc=loc,
                    edgecolor='none',
                    facecolor=cell_color)
        tb._cells[(i + 1, 0)]._text.set_color(header_color)

    # set font size
    if fontsize is not None:
        tb_cells = tb.properties()['child_artists']
        for cell in tb_cells:
            cell.set_fontsize(fontsize)

    # add table to figure
    ax.add_table(tb)

    # set title
    if title is not None:
        if fontsize is not None:
            ax.set_title(title, fontsize=fontsize)
        else:
            ax.set_title(title)

    plt.tight_layout()

    return fig