【问题标题】:Change or set font properties of matplotlib table更改或设置 matplotlib 表的字体属性
【发布时间】:2021-07-09 10:40:48
【问题描述】:

我在 matplotlib 中有一个表格,我想改变它的字体属性,例如更改字体系列和字体大小。我可以根据这里的帖子更改字体大小,如下所示:How to change the table's fontsize with matplotlib.pyplot 。但是,我找不到更改表格其他前端属性的方法,例如字体系列。我查看了返回表的属性,但看不到任何与此处建议的相关性的属性Matplotlib table formatting

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=1)
ax.axis("off")
my_table = ax.table(
     cellText=[[1., 1.25, 1.],
               [1.5, 1., 2.7],
               [3., 2.7, 1.]],
      rowLabels=['row1', 'row2', 'row3'],
      colLabels=['col1', 'col2', 'col3'],
      loc='center'
)

my_table.auto_set_font_size(False)
my_table.set_fontsize(12)

我的问题:如何更具体地更改 matplotlib 表的字体属性(最好不通过 rcParams['font.family'] = 'sans-serif')

【问题讨论】:

    标签: python-3.x matplotlib matplotlib-table


    【解决方案1】:

    我找到了一个粗略的解决方案,其中我更改了每个单元格的 text._fontproperties,如下所示:

    my_table.auto_set_font_size(False)
    my_table.set_fontsize(12)
    
    for cell in my_table._cells:
        text = my_table._cells[cell].get_text()
        text.set_fontstyle('italic')
    
        # Or alternatively
        my_table._cells[cell]._text._fontproperties._family = 'serif'
    

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2017-03-17
      • 1970-01-01
      相关资源
      最近更新 更多