Matplotlib change font size within table (just header) - python

喜夏-厌秋 提交于 2020-06-17 03:37:23

问题


I'd like for the headers of the columns to have a smaller font than the values in the cells so that they're readable (I'll show what I mean in an attached jpeg). Right now everything has the same font size.

Example of the table code:

fig = plt.figure(figsize=(11, 8.27))
ax = fig.add_subplot(111)
ax.axis('off')

index_length = len(well_data_table.index)

table_1 = well_data_table.iloc[0:30]
table_2= well_data_table.iloc[30:60]
table_3 = well_data_table.iloc[60:-1]

q='lightsalmon'
colors3 = [q,q,q,q,q,q,q,q,q,q,q]
the_table1 = ax.table(cellText=table_1.values, colWidths = 
[.1]*len(table_1.columns),
  rowLabels=table_1.index,
  colColours = colors3,
  colLabels=table_1.columns,
  cellLoc = 'center', rowLoc = 'center',
  loc='bottom',
  bbox=[.1, 0, 1, 1]) 

the_table1.auto_set_font_size(False)
the_table1.set_fontsize(8)
the_table1.scale(1, 1)


ax.title.set_text("""TEST""")


pdf.savefig(facecolor='w')

JPEG Example of the issue


回答1:


You could loop over the cells of the table that should get a different font size and set the fontsize in that loop.

cells = the_table1._cells
for cell in the_table1._cells:
    if cell[0] == 0:
        the_table1._cells[cell].set_fontsize(8)



回答2:


Here is an example that helped me iterate through the cells with an index tuple("key" in example) to do things like remove borders or set header(row and col headers in this example):

for key, cell in table.get_celld().items():
    # scrub borders for clean look(see source below)
    cell.set_linewidth(0)

    # adjust format for only header col and row to help with space issues
    # col header on 0, row header on -1.
    if key[0] == 0 or key[1] == -1:
        cell.set_fontsize(6)

I appreciate this post by Bart which helped me remove borders in my specific case.



来源:https://stackoverflow.com/questions/51307031/matplotlib-change-font-size-within-table-just-header-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!