Wednesday, July 3, 2024
HomeLanguagesPythonMatplotlib.axes.Axes.matshow() in Python

Matplotlib.axes.Axes.matshow() in Python

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:

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments