0

I am trying to plot several tables on the same figure in matplotlib, and am unable to change the fontsize of the celltext inside each table. I implemented the solution to similar question (how to change the fontsize of a table not within a subplot) but that didn't work.

Sample Code:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt



figure = plt.figure(figsize=(11, 5.5))
grid = figure.add_gridspec(2, 2)

ax1 = figure.add_subplot(grid[0, 0])
ax2 = figure.add_subplot(grid[1, 0])
ax3 = figure.add_subplot(grid[0, 1])
ax4 = figure.add_subplot(grid[1, 1])

df = pd.DataFrame(np.random.randn(5, 5))

ax1.axis('off')
t = ax1.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax2.axis('off')
t = ax2.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax3.axis('off')
t = ax3.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax4.axis('off')
t = ax4.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)
BigBen
  • 46,229
  • 7
  • 24
  • 40
John Sorensen
  • 710
  • 6
  • 29

1 Answers1

1

The table has the defaults auto_set_font_size to True. Set it to False and pass your font size.

Example:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt



figure = plt.figure(figsize=(11, 5.5))
grid = figure.add_gridspec(2, 2)

ax1 = figure.add_subplot(grid[0, 0])
ax2 = figure.add_subplot(grid[1, 0])
ax3 = figure.add_subplot(grid[0, 1])
ax4 = figure.add_subplot(grid[1, 1])

df = pd.DataFrame(np.random.randn(5, 5))

ax1.axis('off')
t = ax1.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax2.axis('off')
t = ax2.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax3.axis('off')
t = ax3.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.set_fontsize(14)

ax4.axis('off')
t = ax4.table(cellText=df.values, colLabels=df.columns, rowLabels=df.index, loc='center')
t.auto_set_font_size(False)
t.set_fontsize(14)

Output:

result_table

Roxy
  • 1,015
  • 7
  • 20