How to | Plot a Vector Field
You can visualize a vector field by plotting vectors on a regular grid, by plotting a selection of streamlines, or by using a gradient color scheme to illustrate vector and streamline densities. You can also plot a vector field from a list of vectors as opposed to a mapping.
Use VectorPlot to plot vectors in a vector field given by a mapping from
to
:
VectorPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}]Use StreamPlot to plot streamlines:
StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}]Use the StreamPoints option to plot selected streamlines:
StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamColorFunction -> None, StreamPoints -> {{{{-2, 1}, Green}, {{0.5, -1}, Red}}}]Use the StreamPoints option to select streamlines in the plot:
StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamColorFunction -> None, StreamPoints -> {{{{-2, 1}, Green}, {{0.5, -1}, Red}, Automatic}}]Use VectorDensityPlot and StreamDensityPlot to visualize the field densities:
VectorDensityPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}]StreamDensityPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, ColorFunction -> "ThermometerColors"]Use VectorPlot3D to plot a three-dimensional vector field (vectors are colored depending on their magnitude):
VectorPlot3D[{x, y, z}, {x, -3, 3}, {y, -3, 3}, {z, -3, 3}, VectorColorFunction -> "DeepSeaColors"]In addition to simply plotting vector fields, the Wolfram Language allows you to fine-tune these plots. These examples illustrate some of the options that can be applied.
Use VectorStyle to change the type of arrows in VectorPlot:
VectorPlot[{ y, -x}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> {StandardGray, "Disk"}, VectorColorFunction -> None]Use StreamPoints to control the number of streamlines in the plot:
{StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamPoints -> Coarse], StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamPoints -> Fine]}Combine vectors and streamlines into a single plot:
StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, VectorPoints -> Automatic, VectorColorFunction -> None, StreamColorFunction -> None, StreamStyle -> Orange]Use ColorFunction to apply a color scheme based on the density of vectors and streamlines:
StreamDensityPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamColorFunction -> None, StreamStyle -> Black, VectorPoints -> Automatic, VectorColorFunction -> None, VectorStyle -> White, ColorFunction -> "Rainbow"]You can use VectorColorFunction to choose a color scheme and specify a function with which to color the vectors. This makes two plots colored with the "DarkRainbow" color scheme, each according to the functions specified in VectorColorFunction:
{VectorPlot[{ y, -x}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> "Disk", VectorColorFunction -> {"DarkRainbow", Function[{x, y}, x]}], VectorPlot[{ y, -x}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> "Disk", VectorColorFunction -> {"DarkRainbow", Function[{x, y}, x + y]}]}Because some functions used in VectorColorFunction are common, the Wolfram Language allows you to call them as variables. These are represented by integers ranging from 1 to 5, where 1 is the
variable, 2 is the
variable, 3 is the first field component, 4 is the second field component, and 5 is the vector magnitude. To specify these variables, use #n& with VectorColorFunction, where n represents the variable number.
Color the plot according to the second field component (#4&), with the "DarkRainbow" color scheme:
VectorPlot[{ y, -x}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> "Disk", VectorColorFunction -> {"DarkRainbow", #4&}]Color the plot according to the vector magnitude (#5&), also with the "DarkRainbow" color scheme:
VectorPlot[{ y, -x}, {x, -3, 3}, {y, -3, 3}, VectorStyle -> "Disk", VectorColorFunction -> {"DarkRainbow", #5&}]Plot streamlines from a specified point, in one direction:
StreamPlot[{x, -y}, {x, -3, 3}, {y, -3, 3}, StreamColorFunction -> None, StreamPoints -> {{{{-1, 2}, Green}}, Automatic, {Forward, Automatic}}]Use VectorStyle to get 3D effects in VectorPlot3D:
VectorPlot3D[{x, y, z}, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, VectorScaling -> Automatic, VectorColorFunction -> None, VectorStyle -> Red]Specify markers with VectorMarkers:
VectorPlot3D[{x, y, z}, {x, -1, 1}, {y, -1, 1}, {z, -1, 1}, VectorMarkers -> "Arrow"]Related Demonstrations
- Vector Fields: Plot Examples
- Visualizing Vector Fields
- Flow of a Vector Field in 2D
- Vector Fields: Streamline through a Point
- Sources, Saddle Points, and Sinks in Vector Fields
- 3D Vector Fields
- Vector Field Flow through and around a Circle
- Families of Solutions for ODEs
- Integrating a Vector Field along a Curve