更改或设置 matplotlib 的字体属性 table
Change or set font properties of matplotlib table
我在 matplotlib 中有一个 table,我想更改它的字体属性,例如更改字体系列和字体大小。我可以根据 post 更改字体大小,如下所示: How to change the table's fontsize with matplotlib.pyplot
。但是,我找不到更改 table 的其他前端属性的方法,例如字体系列。我查看了返回的 Table 的属性,但看不到此处建议的任何相关属性 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 的字体属性 table 更具体的字体系列(最好不通过 rcParams['font.family'] = 'sans-serif')
我找到了一个粗略的解决方案,在上面的代码的基础上,我按如下方式更改每个单元格的 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'
我在 matplotlib 中有一个 table,我想更改它的字体属性,例如更改字体系列和字体大小。我可以根据 post 更改字体大小,如下所示: How to change the table's fontsize with matplotlib.pyplot 。但是,我找不到更改 table 的其他前端属性的方法,例如字体系列。我查看了返回的 Table 的属性,但看不到此处建议的任何相关属性 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 的字体属性 table 更具体的字体系列(最好不通过 rcParams['font.family'] = 'sans-serif')
我找到了一个粗略的解决方案,在上面的代码的基础上,我按如下方式更改每个单元格的 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'