我如何更改matblolib子图的“框”高度并获取matplotlib表的高度?

问题描述 投票:0回答:1
import matplotlib.pyplot as plt
import pandas as pd
df= pd.DataFrame({'A':['a','b'],'B':[1,2],'C':[1,2]})
fig,ax = plt.subplots()
ax.set_title("title")
ax.set_xlabel('label')
plt.xticks([])
plt.yticks([])
tab = ax.table(rowLabels=range(1,3),colLabels=['A','B','C'],cellText=df.values,loc='center',rowLoc='center')
plt.show()
##My doubt:
function_that_resize_the_box_height( function_that_get_the_table_height(tab) )

这就是'盒子的意思:

enter image description here

python matplotlib height
1个回答
0
投票
一种可能是相反的情况:用fig,ax = plt.subplots(1, 1, figsize = (5, 2.5))设置图形的大小,然后设置表格的bbox参数,使其充满图形:

import matplotlib.pyplot as plt import pandas as pd df= pd.DataFrame({'A':['a','b'],'B':[1,2],'C':[1,2]}) fig,ax = plt.subplots(1, 1, figsize = (5, 2.5)) ax.set_title("title") ax.set_xlabel('label') plt.xticks([]) plt.yticks([]) tab = ax.table(rowLabels=range(1,3),colLabels=['A','B','C'],cellText=df.values,loc='center',rowLoc='center', bbox = [0, 0, 1, 1]) plt.show()

输出:

enter image description here

Check this了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.