# How to display the matrix value and colormap in Matplotlib?

MatplotlibPythonData Visualization

To display the matrix value and colormap in Matplotlib, we can take the following steps −

• Set the figure size and adjust the padding between and around the subplots.
• Create a figure and a set of subplots.
• Initialize max and min values for matrix.
• Plot the values of a 2D matrix or array as color-coded image.
• Iterate each cell of the color-code image and place value at the center.
• To display the figure, use show() method.

## Example

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
min_val, max_val = 0, 5
matrix = np.random.randint(0, 5, size=(max_val, max_val))
ax.matshow(matrix, cmap='ocean')

for i in range(max_val):
for j in range(max_val):
c = matrix[j, i]
ax.text(i, j, str(c), va='center', ha='center')

plt.show()

## Output

Published on 04-Aug-2021 12:52:09