如何使用matplotlib.pyplot更改表格的字体大小? [英] How to change the table's fontsize with matplotlib.pyplot?
问题描述
我正在用pyplot绘制表格,如下所示:
I'm drawing a table with pyplot like this:
sub_axes.table(cellText=table_vals,
colWidths = [0.15, 0.25],
rowLabels=row_labels,
loc='right')
我想更改表格内容的字体大小,发现有一个fontsize
属性,
请参考表"的定义.
I'd like to change the fontsize of table's content, and found there is a fontsize
property,
please ref definition of 'table'.
它变成:
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'
此属性是否已弃用?以及如何使用pyplot更改表格的字体大小?
Is this property deprecated? And how can I change the fontsize of table with pyplot?
推荐答案
我认为文档正在暗示将要设置的参数(注意fontsize
并非其他参数的链接),或者可能有点此刻令人误解.没有fontsize
参数.
I think the documentation is either hinting at a parameter-to-be (notice fontsize
is not a link like the other parameters) or perhaps is a bit misleading at the moment. There is no fontsize
parameter.
浏览源代码,我找到了Table.set_fontsize
方法:
table = sub_axes.table(cellText=table_vals,
colWidths = [0.15, 0.25],
rowLabels=row_labels,
loc='right')
table.set_fontsize(14)
the_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()
这篇关于如何使用matplotlib.pyplot更改表格的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!