Skip to main content
\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)
Mathematics LibreTexts

13.3: Visualizing Two-Dimensional Scalar and Vector Field

 

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

Plotting scalar and vector fields in Python is straightforward, as long as the space is two-dimensional.  Here is an example of how to plot a 3-D surface plot:

code 13.1.png

The scalar field \(f(x,y) = \sin{\sqrt{x^2 + y^2}}\) is given on the right hand side of the zvalues part. The result is shown in Fig. 13.6.

fig 13.6.png

Figure 13.6: Scalar field visualized as a 3-D surface using code 13.1.

And here is how to draw a contour plot of the same scalar field:

code 13.2.png
The clabel command is used here to add labels to the contours. The result is shown in Fig. 13.7.

fig 13.7.png

Figure 13.7:Scalar field visualized as a contour plot using Code 13.2.

If you want more color, you can use imshow, which we already used for CA:

code 13.3 1.png

code 13.3 2.png
The result is shown in Fig. 13.8. Colorful!

fig 13.8.png

Figure 13.8: Scalar field visualized as a color image using Code 13.3.

Finally, a two-dimensional vector field can be visualized using the streamplot function that we used in Section 7.2. Here is an example of the visualization of a vector field v = (vx,vy) = (2x,y−x), with the result shown in Fig. 13.9:

code 13.4.png

code 13.4 2.png

fig 13.9.png

Exercise 13.7

Plot the scalar field \(f(x,y) = \sin{(xy)}\) for \(−4 ≤ x,y ≤ 4\) using Python.

Exercise 13.8

Plot the gradient field of f\((x,y) = \sin{(xy)}\) for \(−4 ≤ x,y ≤ 4\) using Python.

Exercise 13.9

Plot the Laplacian of f\(\x,y) = \sin{(xy)}\) for \(−4 ≤ x,y ≤ 4\) using Python. Compare the result with the outputs of the exercises above.