Python 如何使用 matplotlib.pyplot 更改表格的字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15514005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the table's fontsize with matplotlib.pyplot?
提问by Evans Y.
I'm drawing a table with pyplot like this:
我正在用 pyplot 绘制一个表格,如下所示:
sub_axes.table(cellText=table_vals,
colWidths = [0.15, 0.25],
rowLabels=row_labels,
loc='right')
I'd like to change the fontsize of table's content, and found there is a fontsizeproperty,
please ref definition of 'table'.
我想更改表格内容的字体大小,发现有一个fontsize属性,请参考'table' 的定义。
So it becomes:
所以就变成了:
sub_axes.table(cellText=table_vals,
colWidths = [0.15, 0.25],
rowLabels=row_labels,
fontsize=12,
loc='right')
But when I execute the code, I got an error:
但是当我执行代码时,出现错误:
TypeError: table() got an unexpected keyword argument 'fontsize'
Is this property deprecated? And how can I change the fontsize of table with pyplot?
此属性是否已弃用?以及如何使用 pyplot 更改表格的字体大小?
采纳答案by unutbu
I think the documentation is either hinting at a parameter-to-be (notice fontsizeis not a link like the other parameters) or perhaps is a bit misleading at the moment. There is no fontsizeparameter.
我认为文档要么暗示了一个参数(注意fontsize不是像其他参数那样的链接),要么目前有点误导。没有fontsize参数。
Digging through the source code, I found the Table.set_fontsizemethod:
通过源代码挖掘,我找到了Table.set_fontsize方法:
table = sub_axes.table(cellText=table_vals,
colWidths = [0.15, 0.25],
rowLabels=row_labels,
loc='right')
table.set_fontsize(14)
table.scale(1.5, 1.5) # may help
Here is an example with a grossly exaggerated fontsize just to show the effect.
这是一个使用严重夸大字体大小的示例,只是为了显示效果。
import matplotlib.pyplot as plt
# Based on http://stackoverflow.com/a/8531491/190597 (Andrey Sobolev)
fig = plt.figure()
ax = fig.add_subplot(111)
y = [1, 2, 3, 4, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1]
col_labels = ['col1', 'col2', 'col3']
row_labels = ['row1', 'row2', 'row3']
table_vals = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
the_table = plt.table(cellText=table_vals,
colWidths=[0.1] * 3,
rowLabels=row_labels,
colLabels=col_labels,
loc='center right')
the_table.auto_set_font_size(False)
the_table.set_fontsize(24)
the_table.scale(2, 2)
plt.plot(y)
plt.show()


回答by Yunmeng Zhu
Set the auto_set_font_sizeto False, then set_fontsize(24)
设置auto_set_font_size为False,然后set_fontsize(24)
the_table.auto_set_font_size(False)
the_table.set_fontsize(24)

