如何调整图表的表格?表和图 matplotlib python 的更多空间 [英] How to adjust table for a plot? More space for table and graph matplotlib python
问题描述
我想分开或增加我的桌子和我的图表的距离,这样它们就不会重叠.我想将大小增加到右边并将表格放在那里,但我似乎无法使其工作,而且我找不到将表格偏移 1 行的方法.
I want to separate or increase the distance of my table and my graph so they don't layover. I thought of increasing the size to right and put the table there but I can't seem to make it work, and I can't find a way to offset the table by 1 line.
图表
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
推荐答案
使用 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()
这篇关于如何调整图表的表格?表和图 matplotlib python 的更多空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!