gpt4 book ai didi

Matplotlib 表 : individual column width

转载 作者:行者123 更新时间:2023-12-03 18:23:32 26 4
gpt4 key购买 nike

有没有办法指定 matplotlib 表中各个列的宽度?

我表中的第一列仅包含 2-3 位 ID,我希望此列比其他列小,但我似乎无法让它工作。

假设我有一张这样的表:

import matplotlib.pyplot as plt

fig = plt.figure()
table_ax = fig.add_subplot(1,1,1)

table_content = [["1", "Daisy", "ill"],
["2", "Topsy", "healthy"]]
table_header = ('ID', 'Name','Status')

the_table = table_ax.table(cellText=table_content, loc='center', colLabels=table_header, cellLoc='left')

fig.show()

(别介意奇怪的裁剪,它不会发生在我真正的 table 上。)

我试过的是这样的:
prop = the_table.properties()
cells = prop['child_artists']

for cell in cells:
text = cell.get_text()
if text == "ID":
cell.set_width(0.1)
else:
try:
int(text)
cell.set_width(0.1)
except TypeError:
pass

上面的代码似乎有零影响 - 列仍然一样宽。 ( cell.get_width() 返回 0.3333333333 ,所以我认为 width 确实是单元格宽度......所以我做错了什么?

任何帮助,将不胜感激!

最佳答案

我一直在网上一遍又一遍地搜索类似的problem sollutions。我找到了一些答案并使用了它们,但我没有发现它们很直接。偶然地,我只是在尝试不同的表方法时发现了表方法 get_celld。
通过使用它,您将获得一个字典,其中键是与单元格位置方面的表格坐标相对应的元组。所以通过写

cellDict=the_table.get_celld()
cellDict[(0,0)].set_width(0.1)

您只需寻址左上角的单元格。现在遍历行或列将相当容易。

回答有点晚,但希望其他人可能会有所帮助。

关于Matplotlib 表 : individual column width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com