Skip to main content
 首页 » 编程设计

python之Matplotlib 表落在绘图区域之外

2025年05月04日14kevingrace

我在使用 Matplotlib 的绘图中有一个表格,该表格超出了绘图区域。有没有办法避免这种情况发生,也许是通过偏移量或类似的东西?

import matplotlib.pylab as plt 
 
plt.figure() 
ax=plt.gca() 
 
col_labels=['col1','col2','col3'] 
row_labels=['row1','row2','row3'] 
table_vals=[[11,12,13],[21,22,23],[31,32,33]] 
the_table = plt.table(cellText=table_vals, 
                  colWidths = [0.1]*3, 
                  rowLabels=row_labels, 
                  colLabels=col_labels, 
                  loc='center left') 
plt.text(12,3.4,'Table Title',size=8) 
 
 
plt.show() 

请您参考如下方法:

添加一个bbox参数,这样就可以控制精确的位置,例如:

plt.table(cellText=table_vals, 
                  colWidths = [0.1]*3, 
                  rowLabels=row_labels, 
                  colLabels=col_labels, 
                  loc='center left', 
                  bbox=[0.25, -0.5, 0.5, 0.3]) 

这里是对应的documentation table 和 bbox 的那个.