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

To change the figsize for matshow() in Jupyter notebook, you can set the figure size using plt.figure(figsize=(width, height)) and then specify the figure number in the matshow() method using the fignum parameter.

Steps

  • Create a new figure or activate an existing figure using figure() method with desired figsize
  • Create a dataframe using Pandas
  • Use matshow() method to display an array as a matrix in the figure window
  • The fignum parameter controls which figure to use:
    • If None, create a new figure window with automatic numbering
    • If a nonzero integer, draw into the figure with the given number
    • If 0, use the current axes (or create one if it does not exist)
  • Display the figure using show() method

Method 1: Using plt.figure() with figsize

This method creates a figure with specified dimensions before calling matshow() ?

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

# Create a figure with custom size
plt.figure(figsize=(8, 6))

# Create sample data
df = pd.DataFrame({
    "col1": [1, 3, 5, 7, 1], 
    "col2": [1, 5, 7, 9, 1],
    "col3": [2, 4, 6, 8, 2]
})

# Display correlation matrix
plt.matshow(df.corr(), fignum=1)
plt.title('Correlation Matrix with Custom Figure Size')
plt.colorbar()
plt.show()

Method 2: Using rcParams for Global Settings

Set default figure size globally for all subsequent plots ?

import pandas as pd
import matplotlib.pyplot as plt

# Set global figure size
plt.rcParams["figure.figsize"] = [10, 8]
plt.rcParams["figure.autolayout"] = True

# Create sample data
data = [[1, 0.8, 0.3], [0.8, 1, 0.5], [0.3, 0.5, 1]]
df = pd.DataFrame(data, columns=['A', 'B', 'C'], index=['A', 'B', 'C'])

# Create matshow plot
plt.matshow(df, fignum=1)
plt.title('Matrix Plot with rcParams Figure Size')
plt.colorbar()
plt.show()

Method 3: Direct Array Visualization

You can also use matshow() directly with NumPy arrays ?

import numpy as np
import matplotlib.pyplot as plt

# Create a figure with specific size
plt.figure(figsize=(6, 6))

# Create a sample matrix
matrix = np.random.rand(5, 5)

# Display matrix
plt.matshow(matrix, fignum=1)
plt.title('Random Matrix Visualization')
plt.colorbar(label='Values')
plt.show()

Comparison

Method Scope Best For
plt.figure(figsize=()) Single plot Specific plot customization
plt.rcParams Global/session Consistent sizing across plots
Inline figsize Single plot Quick one-off adjustments

Key Points

  • Always specify fignum=1 (or another integer) to link matshow() with your custom figure
  • Use figsize=(width, height) where values are in inches
  • Add colorbar() to show the scale of values in the matrix
  • The fignum parameter is crucial for applying the custom figure size

Conclusion

Use plt.figure(figsize=(width, height)) with fignum=1 in matshow() for individual plot sizing. For consistent sizing across multiple plots, set plt.rcParams["figure.figsize"] globally.

Updated on: 2026-03-25T20:07:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements