[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.

Introduction

Create contour plots using matplotlib's ** pcolor **, ** imshow **, contour methods.

Postscript: August 28, 2017: ** Added content (4) that mixes pcolor mesh and contour. ** **

Contents

(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

Example (1): Using pcolormesh


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()


Result (1): Diagram using pcolor mesh

t.png

Example (2): Using impshow

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()

Result (2): impshow

t.png


Example (3) Contour diagram using 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.

#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()

Result (3): contour diagram

t.png


Example (4) Mix 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.

#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()

Result (4): Mix of pcolor mesh and contour

fig.png


[Addendum]

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'


Recommended Posts

[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
Example of 3D skeleton analysis by Python
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Python] limit axis of 3D graph with Matplotlib
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
Calculation of technical indicators by TA-Lib and pandas
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation
python --Export 2D histogram as an array by Matplotlib
[Science / technical calculation by Python] Numerical solution of first-order ordinary differential equations, initial value problem, numerical calculation