且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何调整图表的表格?表和图 matplotlib python 的更多空间

更新时间:2023-11-24 08:31:58

使用 bbox

您可以使用 bbox 参数设置表格的位置.它期望一个bbox实例或一个四元组值(左,底部,宽度,高度)(位于轴坐标中).例如

Using bbox

You can set the position of the table using the bbox argument. It expects either a bbox instance or a 4-tuple of values (left, bottom, width, height), which are in axes coordinates. E.g.

plt.table(...,  bbox=[0.0,-0.5,1,0.3])

产生一个与轴一样宽的表( left = 0,width = 1 ),但位于轴的下方( bottom = -0.5,height = 0.3 ).

produces a table that is as wide as the axes (left=0, width=1) but positionned below the axes (bottom=-0.5, height=0.3).

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(4,2)
columns = ('Frequency','Hz')
rows = ['# %d' % p for p in (1,2,3,4)] 

plt.plot(data[:,0], data[:,1], '-') #plot x-y
plt.axis([0, 1, 0, 1.2]) #range for x-y plot
plt.xlabel('Hz')


the_table = plt.table(cellText=data,rowLabels=rows, colLabels=columns,
                      loc='bottom', bbox=[0.0,-0.45,1,.28])
plt.subplots_adjust(bottom=0.3)
plt.show()

您还可以创建一个轴 (tabax) 来放入表格.然后,您可以将 loc 设置为"center" ,关闭轴的旋转并仅使用很小的subplots_adjust bottom参数.

You can also create an axes (tabax) to put the table into. You would then set the loc to "center", turn the axis spines off and only use a very small subplots_adjust bottom parameter.

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(4,2)
columns = ('Frequency','Hz')
rows = ['# %d' % p for p in (1,2,3,4)] 

fig, (ax, tabax) = plt.subplots(nrows=2)

ax.plot(data[:,0], data[:,1], '-') #plot x-y
ax.axis([0, 1, 0, 1.2]) #range for x-y plot
ax.set_xlabel('Hz')

tabax.axis("off")
the_table = tabax.table(cellText=data,rowLabels=rows, colLabels=columns,
                      loc='center')
plt.subplots_adjust(bottom=0.05)
plt.show()