获取Matplotlib表的单元格宽度以覆盖文本和周围图

时间:2017-02-14 07:26:33

标签: python numpy matplotlib

在过去的几个小时里,我一直在为此奋斗,所以任何帮助都将不胜感激。我有一个特定维度的表,每个单元格包含一个特定的短语。每个单元格还有一个相关的浮点值(标准化为1),使用CMAP进行着色。但是,我无法让桌子(和单元格)覆盖封闭图形的整个宽度。以下是我一直在使用的非功能性代码:

fig, ax = plt.subplots(1, 1, figsize=(6,3))



diff = kb_weights.max() - kb_weights.min()
normal = matplotlib.colors.Normalize(kb_weights.min() - diff / 2.0, kb_weights.max() + diff / 2.0)
ax.axis("off")
img = plt.imshow(kb_weights, cmap="Blues")
plt.colorbar()
img.set_visible(False)

table = ax.table(cellText=kb_realized[0:10, :], colLabels=kb_cols_realized,

# The dimensionality of the table is 10x6                                      cellColours=plt.cm.Blues(normal(kb_weights[:10, :])),
                                 loc="center", fontsize=10)

table.auto_set_font_size(False)
table.scale(1.5,1.5)
ax.add_table(table)
plt.savefig("../figures/kb_weights.png")

以下是生成的图像:enter image description here

完全不可理解,我知道,但我真的不确定如何让matplotlib做我想做的事情。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用bbox的{​​{1}}参数来指定其跨越的情节量。例如:

table

将填充整个轴table = ax.table(cellText=..., bbox=(0, 0, 1, 1)) 。您必须对其进行调整,使其跨越您想要的绘图部分。

ax元组提供bbox。左下角为(x0, y0, width, height),右上角为(0, 0)