[Python] How to bold only e.g. the first, second, and second-to-last horizontal lines in a...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working with matplotlib to create a table, and I need to bold specific horizontal lines.

    Here's my current code:


    np_table = table1(data)

    fig, ax = plt.subplots(figsize=(6, 2.5))
    ax.axis('off')

    table = ax.table(cellText=np_table[1:],
    colLabels=np_table[0],
    cellLoc='center',
    loc='center',
    colWidths=[0.5, 0.5])

    table.auto_set_font_size(False)
    table.set_fontsize(9)
    table.scale(0.5, 1.2)

    rows, cols = len(np_table), len(np_table[0])
    for (row, col), cell in table.get_celld().items():
    cell.set_linewidth(0.5)

    if row == 0:
    cell.set_text_props(weight='bold', color='black')

    if col == 0 and row > 0:
    cell.set_text_props(ha='left')

    cell.set_edgecolor('black')

    plt.savefig('table.png', bbox_inches='tight', pad_inches=0.1, dpi=300)
    plt.close(fig)


    I want to make the following modifications:
    Bold the first horizontal line (top border of the header)
    Bold the second horizontal line (bottom border of the header)
    Bold the second-to-last horizontal line
    All other lines should remain thin. How can I achieve this? I've tried adjusting the linewidth for specific cells, but I'm not sure how to target only these particular horizontal lines.

    I'm constrained to use only the following libraries in Python for this task:
    matplotlib, numpy, pandas, seaborn

    No other libraries or external tools are allowed.

    Continue reading...

Compartilhe esta Página