How to adjust table for a plot? More space for table and graph matplotlib python
Asked Answered
C

1

11

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.

Graph

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
Chapatti answered 6/6, 2017 at 23:51 Comment(2)
Maybe you're looking for Tight Layout?Chomp
@yputons Tables are ignored by tight_layout(), so that does not help.Fernandez
F
13

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])

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()

enter image description here

Create dedicated axes

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()

enter image description here

Fernandez answered 7/6, 2017 at 8:22 Comment(4)
Awesome this worked! Curious though, for the second implementation, it created two Figures (1 & 2). 1 with only drawn graph then another window with Graph and Table. Is there away to only have the Graph & Table window show?Chapatti
Both examples are working code and create a single window. So I'm not sure what you mean.Fernandez
When I changed my code and ran the script, it populated 2 windows. 1 with only a graph and 1 with graph and table. Is it only supposed to populate 1 window?Chapatti
To see what it's supposed to do, copy the code from the question without changing it. In both cases a single window is shown up. Now, if you change the code, I cannot help you unless you tell me what you have changed.Fernandez

© 2022 - 2024 — McMap. All rights reserved.