How to change the figsize for matshow() in Jupyter notebook using Matplotlib?

MatplotlibPythonData Visualization

To change the figsize for mathshow, we can use figsize in figure method argument and use fignum in matshow() method.

Steps

  • Create a new figure or activate an existing figure using figure() method.
  • Create a dataframe using Pandas.
  • Use matshow() method to display an array as a matrix in a new figure window.
  • The argument fignum can take the values None, int, or False
    • If *None*, create a new figure window with automatic numbering.
    • If a nonzero integer, draw into the figure with the given number. Create one, if it does not exist.
    • If 0, use the current axes (or create one if it does not exist).
  • To display the figure, use show() method.

Example

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
plt.figure()
df = pd.DataFrame({"col1": [1, 3, 5, 7, 1], "col2": [1, 5, 7, 9, 1]})
plt.matshow(df.corr(), fignum=1)
plt.show()

Output

raja
Published on 15-May-2021 12:15:03
Advertisements