I'm trying to make a table with a dimension of Nx7 where N is a variable.
It is quite challenging to make a proper size of table via matplotlib.
I wanna put it on the center of the plot with a title which is located just above the table.
Here's my code
data = [
['A', 'B', 'C', 'D', 'E', 'F'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200'],
['100%', '200%', 'O', 'X', '1.2%', '100', '200']]
fig, ax1 = plt.subplots(dpi=200)
column_headers = data.pop(0)
row_headers = [x.pop(0) for x in data]
rcolors = np.full(len(row_headers), 'linen')
ccolors = np.full(len(column_headers), 'lavender')
cell_text = []
for row in data:
cell_text.append([x for x in row])
table = ax1.table(cellText=cell_text,
cellLoc='center',
rowLabels=row_headers,
rowColours=rcolors,
rowLoc='center',
colColours=ccolors,
colLabels=column_headers,
loc='center')
fig.tight_layout()
table.scale(1, 0.5)
table.set_fontsize(16)
# Hide axes
ax1.get_xaxis().set_visible(False)
ax1.get_yaxis().set_visible(False)
# Add title
ax1.set_title('{}\n({})'.format(title, subtitle), weight='bold', size=14, color='k')
fig.tight_layout()
plt.savefig(filename)
In my code, there are several problems.
- The title is overlapped on the table.
- The whole figure is somehow right-side shifted from the center. (Left side of resulting image is filled with empty space)
- The size of text in the table is not 16. (much much smaller than the title)