Create contour plots using matplotlib's ** pcolor **, ** imshow **, contour methods.
Postscript: August 28, 2017: ** Added content (4) that mixes pcolor mesh and contour. ** **
(1) Consider $ z = sin (x) + cos (y) $ and draw it in pcolor in the range [0, x, 10], [0, y, 10]. I'm using pcolor mesh. Faster than pcolor ().
(2) Drawing example using impshow
(3) Contour map using contor
(4) A mixture of pcolor mesh and contour
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.05) #Generate x-axis drawing range. 0 to 10 0.In increments of 05.
y = np.arange(0, 10, 0.05) #Generation of y-axis drawing range. 0 to 10 0.In increments of 05.
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y) #Specify the calculation formula to be displayed. Contour lines are made for Z.
plt.pcolormesh(X, Y, Z, cmap='hsv') #Generate contour maps. Specify the coloring rule with cmap.
#plt.pcolor(X, Y, Z, cmap='hsv') #Generate contour maps. Specify the coloring rule with cmap.
pp=plt.colorbar (orientation="vertical") #Color bar display
pp.set_label("Label", fontname="Arial", fontsize=24) #Color bar label
plt.xlabel('X', fontsize=24)
plt.ylabel('Y', fontsize=24)
plt.show()
If you want to visualize a two-dimensional array quickly, use matplotlib.pyplot.imshow ().
import numpy as np
import matplotlib.pyplot as plt
z=np.zeros([100,100])
for i in range(100):
for j in range(100):
z[i,j] = i+j
plt.imshow(z)
plt.colorbar () #Color bar display
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.05) #Generate x-axis drawing range. 0 to 10 0.In increments of 05.
y = np.arange(0, 10, 0.05) #Generation of y-axis drawing range. 0 to 10 0.In increments of 05.
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y) #Specify the calculation formula to be displayed. Contour lines are made for Z.
#Generate contour maps.
cont=plt.contour(X,Y,Z, 5, Vmax=1,colors=['black'])
cont.clabel(fmt='%1.1f', fontsize=14)
pp=plt.colorbar (orientation="vertical") #Color bar display
pp.set_label("Label", fontname="Arial", fontsize=24) #Color bar label
plt.xlabel('X', fontsize=24)
plt.ylabel('Y', fontsize=24)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.05) #Generate x-axis drawing range. 0 to 10 0.In increments of 05.
y = np.arange(0, 10, 0.05) #Generation of y-axis drawing range. 0 to 10 0.In increments of 05.
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y) #Specify the calculation formula to be displayed. Contour lines are made for Z.
#Generate contour maps.
cont=plt.contour(X,Y,Z, 5, vmin=-1,vmax=1, colors=['black'])
cont.clabel(fmt='%1.1f', fontsize=14)
plt.xlabel('X', fontsize=24)
plt.ylabel('Y', fontsize=24)
plt.pcolormesh(X,Y,Z, cmap='cool') #Color contour map
pp=plt.colorbar (orientation="vertical") #Color bar display
pp.set_label("Label", fontsize=24)
plt.show()
Specify the pattern with ** cmap ** of the color map specification. Dozens of patterns are available. Samples can be found here [https://matplotlib.org/examples/color/colormaps_reference.html):
'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'