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

标签 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 做我想做的事。

谢谢!

最佳答案

您可以使用tablebbox 参数来指定它跨越多少图。例如:

table = ax.table(cellText=..., bbox=(0, 0, 1, 1))

将使它填满整个轴 ax。您必须对其进行调整,使其跨越您想要的绘图部分。

bbox 元组给出了(x0, y0, width, height)。左下角是(0, 0),右上角是(1, 1)

关于python - 获取 Matplotlib 表格的单元格宽度以覆盖文本和周围的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42220173/

相关文章:

python - AllenNLP 共指解析的多 GPU 训练

python - Pandas DataFrame [cell=(label,value)],分为 2 个独立的数据框

python - Matplotlib 在使用 "Times New Roman"时设置粗体标题

python - 值错误: zero-size array to reduction operation minimum which has no identity

python - 测试 Numpy 数组是否包含给定的行

python - 修复对数刻度中的 x 和 y 轴格式

python - Tensorflow 神经网络在训练后总是有 50% 的把握

python - 将 Pandas 系列转换为整数

python - 在 Python 中的 API 网关请求中使用 AWS Cognito 访问 token

numpy - "import numpy as cp"是处理非 GPU 情况的好习惯吗?