Data visualization helps business users gain insight into large amounts of data. By visually representing data in graphs and charts, users can identify the patterns, relationships, and errors in the data and make predictions accordingly.
In Julia's ecosystem, Plots.jl
is the standard and feature-rich meta-package for data visualization. While using the same syntax, it allows users to interact with various plotting libraries called the backends, such as pyplot, HDF5, and others.
A vector in Julia is an ordered collection of elements allowing duplicates and exposed as a one-dimensional array.
On the other hand, a vector field is a distribution of vectors in space and time. Vector fields can be visualized as a collection of arrows with a given magnitude and direction, each attached to a point on the plane. It is common for vector fields to be used for modeling in a 3-D format, such as abstracting the speed and direction of moving fluids.
Let's find out how to plot a vector and vector field using the Plots.jl
package.
This example shows how to draw a line plot including two 1-D vectors.
ENV["GKSwstype"] = "nul"using Plotsx = 1:10y = sin.(x)z = cos.(x)p = plot(x,y,title = "Plotting vectors",labels = "sin")p = plot!(z,linecolor =:green,line =:dashdot,labels = "cos")savefig(p,"/usercode/output/plot.png")
Now, we'll explain the code above:
Line 1: Initialize the environment variable GKSwstype
to minimize the loading and compilation time of the package Plots.jl
.
Line 2: Load the module Plots.jl
.
Line 3: Create a range from 0 to 10 representing the x
coordinates.
Line 4: Create a 1-D vector that represents the y
coordinates where each element is evaluated to the function sin(x)
.
Line 5: Create a 1-D vector that represents the z
coordinates where each element is evaluated to the function cos(x)
.
Lines 7–11: Invoke the plot()
function in order to create a plot object while specifying as parameters the previously defined variables x
and y
as well as a title to add to the plot, and a label "sin"
to set for the line plotted.
Lines 12–16: Invoke the function plot!
in order to add additional drawings to the existing plot while specifying as parameters the previously defined variable z
as well as the type of line to be plotted, and a label "cos"
to set for this line.
Line 18: Save the generated output, which is the plot figure.
This example illustrates how to draw a vector field.
ENV["GKSwstype"] = "nul"using Plotsu, v = rand(10),rand(10);print("<br> u=",u)print("<br> v=",v)p = quiver(rand(10), gradient=(u,v), arrowscale=0.3, headsize=0.2)savefig(p,"/usercode/output/plot.png")
Let's explain the code presented above:
Line 1: Initialize the environment variable GKSwstype
to minimize the loading and compilation time of the package Plots.jl
.
Line 2: Load the module Plots.jl
.
Line 3: Initialize the variables u
and v
using an array of values between 0 and 1 randomly generated through the function rand
.
Lines 4–5: Display the values of the variables u
and v
.
Line 6: Invoke the main function of this script: quiver
. This function that draws the vector field takes the following as parameters:
rand(10)
: Represents the 10 points where the arrows are to be drawn.
gradient=(u,v)
: Represents the slope and the size of the generated arrows.
arrowscale=0.3, headsize=0.2
: Are arguments that give a scale factor for the size of the arrows and their heads.
Line 7: Save the generated output, which is the figure.
Free Resources