Matplotlib a multiplatform data visualization library built on NumPy arrays, and designed to work with the broader SciPy stack. Matplotlib has the ability to play well with many operating systems and graphics backends as well. matplotlib.pyplot can also be used for MATLAB-like plotting framework.
Plotting a quadrilateral mesh
pcolormesh() function of the pyplot module is used which is similar to pcolor() function, but pcolor returns PolyCollection whereas pcolormesh returns matplotlib.collections.QuadMesh. pcolormesh is much faster and hence can deal with larger arrays.
Syntax : pcolormesh(cmap = [None | Colormap], alpha = [0<=scalar<=1 | None], edgecolors = [None | color | ‘face’], shading = [‘gouraud’ | ‘flat’], norm = [None | Normalize], vimax = [scalar | None], vimin = [scalar | None])
Parameters:
- cmap : It can be None or matplotlib has a number of built-in colormaps accessible via matplotlib.cm.get_cmap
- alpha : It can be None or alpha value between 0 to 1.
- edgecolors :
- If its None, edges will not be visible.
- ‘face’ represents the same color as faces.
- color sequence will set a color.
- shading : It can be either ‘flat’ or ‘gouraud’
- norm : If its None defaults to normalize().
- vimax : It can be either None or the scalar value.
- vimin : It can be either None or the scalar value. ( vimax and vimin are used in conjunction with normalize data)
Example 1 :
import matplotlib.pyplot as plt import numpy as np x1, y1 = 0.1 , 0.05 # generate 2-D grids for the # x & y bounds y, x = np.mgrid[ slice ( - 3 , 3 + y1, y1), slice ( - 3 , 4 + x1, x1)] z = ( 1 - x / 2. + x * * 5 + y * * 3 ) * np.exp( - x * * 2 - y * * 2 ) # Remove the last value from the # z array as z must be inside x # and y bounds. z = z[: - 1 , : - 1 ] z_min, z_max = - np. abs (z). max (), np. abs (z). max () plt.subplot() plt.pcolormesh(x, y, z, cmap = 'YlGn' , vmin = z_min, vmax = z_max, edgecolors = 'face' , shading = 'flat' ) plt.title( 'pcolormesh_example' ) # set the limits of the plot # to the limits of the data plt.axis([x. min (), x. max (), y. min (), y. max ()]) plt.colorbar() plt.show() |
Output :
Example 2 :
import matplotlib.pyplot as plt import numpy as np x = np.array([[ 0 , 1 , 2 , 3 ], [ 0 , 1 , 2 , 3 ], [ 0 , 1 , 2 , 3 ], [ 0 , 1 , 2 , 3 ]]) y = np.array([[ 0.0 , 0.0 , 0.0 , 0 ], [ 1.0 , 1.0 , 1.0 , 1 ], [ 2.0 , 2.0 , 2.0 , 2 ], [ 3 , 3 , 3 , 3 ]]) values = np.array([[ 0 , 0.5 , 1 ], [ 1 , 1.5 , 2 ], [ 2 , 2.5 , 3 ]]) fig, ax = plt.subplots() ax.pcolormesh(x, y, values) ax.set_aspect( 'equal' ) ax.set_title( "pcolormesh_example2" ) |
Output :