Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 232
Jonathan Avatar asked Jun 06 '17 23:06

Jonathan


People also ask

How do you change the range of a plot in Matplotlib?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do I increase the gap between subplots?

We can use the plt. subplots_adjust() method to change the space between Matplotlib subplots. The parameters wspace and hspace specify the space reserved between Matplotlib subplots. They are the fractions of axis width and height, respectively.


1 Answers

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

like image 110
ImportanceOfBeingErnest Avatar answered Sep 18 '22 12:09

ImportanceOfBeingErnest