There are several ways to visualize 2-Dimensional gridded data using matplotlib.pyplot:
In the following example, we will plot the function $z=exp(-sin(2x)^{2}-cos(2y)^2)-0.5$.
Given each pair of x and y values, z (height) can be computed. Therefore the function z can be plotted on a "map" with axes along x and y using contour or surface.
First we need to generate the data of z using many combinations x and y values covering the ranges of x and y values of interest.
The most efficient way to do this is to specific 1-D arrays of x(0:n) and y(0:m) (for example, using np.linspace), then use np.meshgrid to "expand" the 1-D arrays into 2-D arrays of size (0:m,0:n):
import numpy as np
# let's first use a smaller array to demonstrate what meshgrid does --
x=np.linspace(-1,1,3)
y=np.linspace(-1,1,5)
xx,yy=np.meshgrid(x,y)
print(x)
print(xx.shape)
print(xx)
[-1. 0. 1.] (5, 3) [[-1. 0. 1.] [-1. 0. 1.] [-1. 0. 1.] [-1. 0. 1.] [-1. 0. 1.]]
print(y)
print(yy.shape)
print(yy)
[-1. -0.5 0. 0.5 1. ] (5, 3) [[-1. -1. -1. ] [-0.5 -0.5 -0.5] [ 0. 0. 0. ] [ 0.5 0.5 0.5] [ 1. 1. 1. ]]
# Now we use finer grid (larger arrays) to continue our example:
x=np.linspace(-1,1,25)
y=np.linspace(-1,1,25)
xx,yy=np.meshgrid(x,y)
print(xx.shape)
# compute function z using the grids constructed by xx and yy
z=np.exp(-np.sin(2*xx)**2-np.cos(2*yy)**2)-0.5
print(z.shape)
(25, 25) (25, 25)
CS=plt.contour(x,y,z,cmap=...,levels=...,colors=...)
reference: http://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html#matplotlib.pyplot.contour
import matplotlib.pyplot as plt
CS = plt.contour(x, y, z)
plt.title(r'z=exp(-sin(2x)$^2$-cos(2y)$^2$)-0.5')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
CS=plt.contour(x,y,z,cmap=...,levels=...,colors=...)
Matplotlib provides many pre-defined color map options. Here are some examples:
Use cmap=cm.colormapname in the contour (and contourf) command to change color map. (Default is cm.jet)
More color map choices here: https://matplotlib.org/examples/color/colormaps_reference.html
import matplotlib.cm as cm
CS = plt.contour(x, y, z,cmap=cm.ocean)
plt.show()
plt.clabel(CS, inline=1, fontsize=10)
CS = plt.contour(x, y, z,cmap=cm.ocean)
plt.clabel(CS, inline=1, fontsize=10)
plt.show()
CS=plt.contour(x,y,z,cmap=...,levels=...,colors=...)
CS = plt.contour(x, y, z, levels=np.linspace(-0.5,0.5,11),colors='r')
plt.clabel(CS,fontsize=7)
plt.show()
Similar to contour but fill the space between contours with solid colors.
CS=plt.contourf(x,y,z,cmap=...,levels=...)
CS = plt.contourf(x, y, z, levels= np.linspace(-0.5,0.5,11),cmap=cm.rainbow)
plt.show()
plt.colorbar(CS, orientation=...)
CS = plt.contourf(x, y, z, levels= np.linspace(-0.5,0.5,11),cmap=cm.jet)
plt.colorbar(CS,orientation='vertical')
plt.show()
You can also overlay filled contour with contour lines:
CS = plt.contourf(x, y, z, levels=np.linspace(-0.5,0.5,11),cmap=cm.jet)
CS2 = plt.contour(x, y, z, levels=np.linspace(-0.5,0.5,11),colors='k')
plt.show()
plt.pcolormesh(xx, yy, z, cmap=...,shading=...,edgecolors=...)
CS = plt.pcolormesh(xx, yy, z, cmap=cm.jet,shading='flat')
plt.show()
CS = plt.pcolormesh(xx, yy, z, cmap=cm.jet,shading='flat',edgecolors='grey')
plt.show()
CS = plt.pcolormesh(xx, yy, z, cmap=cm.jet,shading='gouraud')
plt.show()
Several steps are involved to show the function z(x,y) with 3D effects
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xx, yy, z, cmap=cm.jet)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title(r'z=exp(-sin(2x)$^2$-cos(2y)$^2$)-0.5')
plt.show()
Here you’ll find a host of example plots with the code that generated them: http://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py