Matplotlib表格:单个列宽
有没有办法指定matplotlib表中各个列的宽度 我表格中的第一列只包含2-3位数的ID,我希望这个列比其他列小,但我似乎无法让它工作 假设我有一张这样的桌子:Matplotlib表格:单个列宽,matplotlib,Matplotlib,有没有办法指定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"
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()
(别管奇怪的裁剪,它不会发生在我真正的桌子上。)
我试过的是:
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.3333
,因此我认为width
确实是单元格宽度……那么我做错了什么
感谢您的帮助!条件text==“ID”
总是False
,因为cell.get_text()
返回的是文本而不是字符串:
for cell in cells:
text = cell.get_text()
print text, text=="ID" # <==== here
if text == "ID":
cell.set_width(0.1)
else:
try:
int(text)
cell.set_width(0.1)
except TypeError:
pass
我一直在网络上一遍又一遍地搜索类似的问题解决方案。我找到了一些答案并使用了它们,但我没有直接找到它们。我只是在尝试不同的表方法时偶然发现了表方法。
通过使用它,您可以得到一个字典,其中键是元组,对应于单元格位置的表坐标
cellDict=the_table.get_celld()
cellDict[(0,0)].set_width(0.1)
您只需寻址左上角的单元格。现在在行或列上循环将相当容易
回答有点晚,但希望其他人能得到帮助。只是为了完成。列标题以(0,0)…(0,n-1)开头。行标题以(1,-1)…(n,-1)开头
守则:
for key, cell in the_table.get_celld().items():
print (str(key[0])+", "+ str(key[1])+"\t"+str(cell.get_text()))
啊,我明白了。但是如何指定我只想更改第一列单元格的宽度?单元格
是一个列表,我看不到顺序。
---------------------------------------------
| ColumnHeader (0,0) | ColumnHeader (0,1) |
---------------------------------------------
rowHeader (1,-1) | Value (1,0) | Value (1,1) |
--------------------------------------------
rowHeader (2,-1) | Value (2,0) | Value (2,1) |
--------------------------------------------
for key, cell in the_table.get_celld().items():
print (str(key[0])+", "+ str(key[1])+"\t"+str(cell.get_text()))