【发布时间】:2021-04-22 00:11:32
【问题描述】:
我正在尝试制作一个尺寸为 Nx7 的表格,其中 N 是一个变量。
通过 matplotlib 制作合适大小的表格非常具有挑战性。
我想把它放在情节的中心,标题就在桌子的正上方。
这是我的代码
data = [
['A', 'B', 'C', 'D', 'E', 'F'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200']]
fig, ax1 = plt.subplots(dpi=200)
column_headers = data.pop(0)
row_headers = [x.pop(0) for x in data]
rcolors = np.full(len(row_headers), 'linen')
ccolors = np.full(len(column_headers), 'lavender')
cell_text = []
for row in data:
cell_text.append([x for x in row])
table = ax1.table(cellText=cell_text,
cellLoc='center',
rowLabels=row_headers,
rowColours=rcolors,
rowLoc='center',
colColours=ccolors,
colLabels=column_headers,
loc='center')
fig.tight_layout()
table.scale(1, 0.5)
table.set_fontsize(16)
# Hide axes
ax1.get_xaxis().set_visible(False)
ax1.get_yaxis().set_visible(False)
# Add title
ax1.set_title('{}\n({})'.format(title, subtitle), weight='bold', size=14, color='k')
fig.tight_layout()
plt.savefig(filename)
在我的代码中,有几个问题。
- 标题重叠在桌子上。
- 整个图形从中心向右偏移。 (生成图像的左侧被空白区域填充)
- 表格中的文字大小不是16。(比标题小很多)
谢谢。
【问题讨论】:
-
这能回答你的问题吗? Matplotlib table formatting column width
-
您是否正在运行最新版本的 matplotlib(当前为
3.3.3)? -
@JoshuaFooJF 它有助于解决某些问题。感谢您的参考
标签: matplotlib