如何调整情节表?更多空间用于表和图形matplotlib python

Jon*_*han 7 python matplotlib python-3.x

我想分开或增加我的桌子和我的图表的距离,以便它们不会停留.我想把尺寸增加到右边并将桌子放在那里,但我似乎无法使它工作,我找不到一种方法将表格偏移1行.

图形

global dataread
global top4
global iV
top4mod = [] #holder for table, combines amplitude and frequency (bin*3.90Hz)


plt.plot(x1, fy1, '-') #plot x-y
plt.axis([0, 500, 0, 1.2]) #range for x-y plot
plt.xlabel('Hz')

columns = ('Frequency','Hz')
rows = ['# %d' % p for p in (1,2,3,4)] #top4
colors = 'C0'
print(len(rows))
print(len(str(top4)))
print(top4)


iV=[d*bins for d in iV]  # convert bins into frequency

i=0;
FirstCol = [4, 3, 2, 1]
while i < 4:
    Table.append([iV[i]] + [top4[i]])#[FirstCol[i]]
    i = i+1

cell_text = []
n_rows = len(Table)
index = np.arange(len(columns)) + 1  #0.3 orginal
bar_width = 0.4

y_offset = np.array([0.0] * len(columns))

for row in range(n_rows):
    #plt.bar(index, Table[row], bar_width, bottom=y_offset, color='C0')  #dont use this
    y_offset = y_offset + Table[row]
    cell_text.append(['%1.1f' % p for p in y_offset])

the_table = plt.table(cellText=Table,rowLabels=rows, colLabels=columns,loc='bottom')
#plt.figure(figsize=(7,8))


# Adjust layout to make room for the table:
plt.subplots_adjust(bottom=0.2) #left=0.2, bottom=0.2


plt.show() #display plot
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 9

运用 bbox

您可以使用bbox参数设置表的位置.它需要一个bbox实例或四元组值(left, bottom, width, height),它们位于轴坐标中.例如

plt.table(...,  bbox=[0.0,-0.5,1,0.3])
Run Code Online (Sandbox Code Playgroud)

生成一个与轴(left=0, width=1)一样宽但位于轴(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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

创建专用轴

您还可以创建一个轴(tabax)来放入表格.然后,您将设置loc"center",关闭轴脊,仅使用非常小的subplots_adjust底部参数.

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述