Friday, February 6, 2026
HomeLanguagesMatplotlib.pyplot.matshow() in Python

Matplotlib.pyplot.matshow() in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.

matplotlib.pyplot.matshow()

matplotlib.pyplot.matshow() function is used to represent an array as a matrix in a new figure window. the upper left-hand corner is set as the origin and the rows (first dimension of the array) are displayed in a horizontal form. The aspect ratio of the figure window is set according to the array to avoid short and narrow figures. The x-axis Tick labels are placed at the top.
 

Syntax: matplotlib.pyplot.matshow(A, fignum=None, **kwargs)
Parameters: 

  1. A:: It is an array like object that represents the matrix. It is a required parameter.
  2. fignum: It accepts three values namely ‘None’, ‘False’ or an integer value. If the value is set to None it would create a new window of the figure with automated numbering. If the value is a non-zero integer then it is drawn into the figure respective to the given number or it creates it if it doesn’t exist. If the ‘0’ is set as the value of this parameter then it uses the current axes or it creates one if it doesn’t exist. 

Returns: It returns an image of Axesimage class.
Other parameters: It also accepts the imshow argument for showing image.

Example 1:  

Python3




import matplotlib.pyplot as plot
import numpy as np
 
# an array with linearly increasing values
array = np.diag(range(20))
 
plot.matshow(array)
 
plot.show()


Output: 

matplotlib.pyplot.matshow()

Example 2: 

Python3




import numpy as np
import matplotlib.pyplot as plt
 
alphabets = ['A', 'B', 'C', 'D', 'E']
 
# randomly generated array
random_array = np.random.random((5, 5))
 
figure = plt.figure()
axes = figure.add_subplot(111)
 
# using the matshow() function
caxes = axes.matshow(random_array, interpolation ='nearest')
figure.colorbar(caxes)
 
axes.set_xticklabels(['']+alphabets)
axes.set_yticklabels(['']+alphabets)
 
plt.show()


Output: 

matplotlib.pyplot.matshow()

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32489 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6861 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12073 POSTS0 COMMENTS
Shaida Kate Naidoo
6995 POSTS0 COMMENTS
Ted Musemwa
7236 POSTS0 COMMENTS
Thapelo Manthata
6946 POSTS0 COMMENTS
Umr Jansen
6929 POSTS0 COMMENTS