How to change a table's fontsize with matplotlib.pyplot?

MatplotlibPythonData Visualization

To change a table's fontsize with matplotlib, we can use set_fontsize() method.

Steps

  • Create a figure and a set of subplots, nrows=1 and ncols=1.
  • Create random data using numpy.
  • Create columns value.
  • Make the axis tight and off.
  • Initialize a variable fontsize to change the font size.
  • Set the font size of the table using set_font_size() method.
  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axs = plt.subplots(1, 1)
data = np.random.random((10, 3))
columns = ("Column I", "Column II", "Column III")
axs.axis('tight')
axs.axis('off')
the_table = axs.table(cellText=data, colLabels=columns, loc='center')
the_table.auto_set_font_size(False)
the_table.set_fontsize(10)
plt.show()

Output

raja
Published on 15-May-2021 12:09:46
Advertisements