首页 > 解决方案 > Matplotlib 在表格中更改字体大小(只是标题) - python

问题描述

我希望列的标题具有比单元格中的值更小的字体,以便它们可读(我将在附加的 jpeg 中说明我的意思)。现在一切都具有相同的字体大小。

表代码示例:

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 问题示例

标签: pythonmatplotlibfontssize

解决方案


您可以遍历应该获得不同字体大小的表格单元格并在该循环中设置字体大小。

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

推荐阅读