Matplotlib Row heights table property
Question:
I have tried every command and piece of documentation I can find. How do I set the the height of the rows here.
from pylab import *
# Create a figure
fig1 = figure(1)
ax1_1 = fig1.add_subplot(111)
# Add a table with some numbers....
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
# Format table numbers as string
tab_2 = [['%.2f' % j for j in i] for i in tab]
y_table = plt.table(cellText=tab_2,colLabels=['Col A','Col B'], colWidths = [.5]*2, loc='center')
y_table.set_fontsize(34)
show()
Answers:
You could use ytable.scale
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
tab2 = [['%.2f' % j for j in i] for i in tab]
ytable = plt.table(cellText=tab2, colLabels=['Col A','Col B'],
colWidths=[.5]*2, loc='center')
ytable.set_fontsize(34)
ytable.scale(1, 4)
plt.show()
yields
The above answer works, but is kinda a cheat and doesn’t provide any flexibility, eg. you can’t make the top row higher than the others. You can explicitly set the height of each cell in a row with the get_celld()
method and set_height()
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
tab2 = [['%.2f' % j for j in i] for i in tab]
colLabels=['Col A','Col B']
ytable = ax.table(cellText=tab2, colLabels=colLabels,
colWidths=[.5]*2, loc='center')
cellDict = ytable.get_celld()
for i in range(0,len(colLabels)):
cellDict[(0,i)].set_height(.3)
for j in range(1,len(tab)+1):
cellDict[(j,i)].set_height(.2)
ytable.set_fontsize(25)
ax.axis('off')
ax.axis('off')
plt.show()
I think the following is simpler than what @JuneSkeeter proposed if you know which row you want to be larger. It avoids having to loop twice. This makes the first row (i.e. row index 0) 0.3
.
for r in range(0, len(colLabels)):
cell = ytable[0, r]
cell.set_height(0.3)
I have tried every command and piece of documentation I can find. How do I set the the height of the rows here.
from pylab import *
# Create a figure
fig1 = figure(1)
ax1_1 = fig1.add_subplot(111)
# Add a table with some numbers....
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
# Format table numbers as string
tab_2 = [['%.2f' % j for j in i] for i in tab]
y_table = plt.table(cellText=tab_2,colLabels=['Col A','Col B'], colWidths = [.5]*2, loc='center')
y_table.set_fontsize(34)
show()
You could use ytable.scale
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
tab2 = [['%.2f' % j for j in i] for i in tab]
ytable = plt.table(cellText=tab2, colLabels=['Col A','Col B'],
colWidths=[.5]*2, loc='center')
ytable.set_fontsize(34)
ytable.scale(1, 4)
plt.show()
yields
The above answer works, but is kinda a cheat and doesn’t provide any flexibility, eg. you can’t make the top row higher than the others. You can explicitly set the height of each cell in a row with the get_celld()
method and set_height()
:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
tab = [[1.0000, 3.14159], [1, 2], [2, 1]]
tab2 = [['%.2f' % j for j in i] for i in tab]
colLabels=['Col A','Col B']
ytable = ax.table(cellText=tab2, colLabels=colLabels,
colWidths=[.5]*2, loc='center')
cellDict = ytable.get_celld()
for i in range(0,len(colLabels)):
cellDict[(0,i)].set_height(.3)
for j in range(1,len(tab)+1):
cellDict[(j,i)].set_height(.2)
ytable.set_fontsize(25)
ax.axis('off')
ax.axis('off')
plt.show()
I think the following is simpler than what @JuneSkeeter proposed if you know which row you want to be larger. It avoids having to loop twice. This makes the first row (i.e. row index 0) 0.3
.
for r in range(0, len(colLabels)):
cell = ytable[0, r]
cell.set_height(0.3)