Open In App
Related Articles

Matplotlib.axes.Axes.matshow() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.matshow() Function

The Axes.matshow() function in axes module of matplotlib library is also used to plot the values of a 2D matrix or array as color-coded image.
Syntax:
Axes.matshow(self, Z, **kwargs)
Parameters: This method accept the following parameters that are described below:
  • z: This parameter contains the matrix which is to be displayed.
Returns: This returns the following:
  • image : This returns the AxesImage
Below examples illustrate the matplotlib.axes.Axes.imshow() function in matplotlib.axes: Example-1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
      
dx, dy = 0.015, 0.05
y, x = np.mgrid[slice(-4, 4 + dy, dy),
                slice(-4, 4 + dx, dx)]
z = (1 - x / 3. + x ** 5 + y ** 5) * np.exp(-x ** 2 - y ** 2)
z = z[:-1, :-1]
  
z_min, z_max = -np.abs(z).max(),
np.abs(z).max()
    
fig, ax = plt.subplots()
      
c = ax.matshow(z, cmap ='Greens',
               vmin = z_min,
               vmax = z_max,
              extent =[x.min(),
                       x.max(), 
                       y.min(),
                       y.max()],
              interpolation ='nearest',
               origin ='lower')
  
fig.colorbar(c, ax = ax)
ax.set_title('matplotlib.axes.Axes.matshow() Examples\n')
plt.show()

                    
Output: Example-2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
  
  
def samplemat(dims):
    """Make a matrix with all zeros and increasing 
       elements on the diagonal"""
  
    aa = np.zeros(dims)
    for i in range(min(dims)):
        aa[i, i] = np.sin(i**3)**2 + i**3
    return aa
  
  
# Display matrix  
fig, ax = plt.subplots()
ax.matshow(samplemat((9, 9)), cmap ="Accent")
  
ax.set_title('matplotlib.axes.Axes.matshow() Examples\n')
plt.show()

                    
Output:

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 13 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials