2023-08-24
The plot function suffices for graphing simple curves of
the form
.
For more complex plots involving functions of several variables or
vector-valued functions, the Plots package provides
additional functions.
This final lesson describes a range of additional plot types provided
by Plots. By the end of this lesson, you will be able to
create
Recall that the savefig function is used to save a
figure to a graphics file, such as a .png or
.jpg file.
In this and the following section, our examples will assume that the command
using Plots, LaTeXStrings
has been executed beforehand so that the necessary graphics functions are available, as well as support for LaTeX.
The basic plot command can be used to plot a parametric
curve, as in the following example that plots an ellipse.
t = range(-1, 1, length=301)
x = 3 * cospi.(t)
y = sinpi.(t)
plot(x, y, xlabel="x", ylabel="y",
label="ellipse", aspect_ratio=:equal)
The output is shown below.
By setting aspect_ratio=:equal we ensure that the scale
on the x-axis is the same as the scale on the y-axis.
If the y-values of a plot vary over several orders of magnitude, then
a log scale on the y-axis will often allow the behaviour of the curve to
be seen more clearly. In the next example, we set
yscale=:log10 in the lower subplot. Note also the
L"..." syntax for a LaTeX string.
x = range(0, 200, length=301)
y1 = 1 ./ (1 .+ x).^2
y2 = 1 ./ (1 .+ x).^4
p1 = plot(x, [y1 y2], label=[L"(1+x)^{-2}" L"(1+x)^{-4}"])
p2 = plot(x, [y1 y2], yscale=:log10, xlabel=L"x",
label=[L"(1+x)^{-2}" L"(1+x)^{-4}"])
plot(p1, p2, layout=(2, 1))
The output is as follows.
We can also set xscale=:log10 to use a log scale on the
x-axis.
Recall that a contour or level set of a real-valued function is a curve of the form for some constant . A standard way of visualising is to plot the family of contours determined by a sequence of values for .
If then we can create a contour plot of as follows.
f(x, y) = x^3 - 3x + y^2
x = range(-3, 3, length=250)
y = range(-4, 4, length=200)
z = f.(x', y)
contour(x, y, z, xlabel=L"x", ylabel=L"y", levels=20)
Here, z is a
matrix with z[i,j] = f(x[j], y[i]), since x'
is
and y has length
.
The output is shown below.
Replacing contour with contourf produces a
filled contour plot.
Another way to visualise a real-valued function
is to plot the graph,
,
in xyz-space. With the vectors x, y and
z defined as in the contour plot example above,
surface(x, y, z, xlabel=L"x", ylabel=L"y", zlabel=L"z")
produces the following output.
The view_angle attribute is a tuple consisting to two
angles, the azimumth and elevation, that define the
direction from which the plot is viewed. Azimuth is measured
counterclockwise from the negative y-axis in the xy-plane. Elevation is
the angle above the xy-plane. Both are measured in degrees, and the
default view_angle is (30,30). In the
command
surface(x, y, z, xlabel=L"x", ylabel=L"y", zlabel=L"z",
view_angle=(135,10))
we change the azimuth to degrees and the elevation to degrees, resulting in the following plot.
A quiver plot is used to visualise a 2D vector field, that is, a vector valued function In the example below, and .
x = range(-1, 1, length=15)
y = range(-π, π, length=15)
X = [ x[j] for i = 1:15, j=1:15 ]
Y = [ y[i] for i = 1:15, j=1:15 ]
U = sin.(Y)
V = -X
scale = 0.1
sU = scale * U
sV = scale * V
quiver(X, Y, quiver=(sU, sV))
At each point (X[i,j], Y[i,j]), an arrow is drawn from
this point to (X[i,j] + sU[i,j], Y[i,j] + sV[i,j]).
The Plots package relies on a software backend
to create its graphical output. The default backend is called GR and is the one most
likely to work reliably out of the box. Usually the output looks pretty
much the same for any choice of backend, but sometimes a particular
backend might offer extra features. For example, the Plotly
backend supports mouse interaction to change the viewing angle in a 3D
plot. If you install the Plotly package, do
using Plots
plotly()
and then create a surface plot as described above, then the plot
should open in your default web browser, and you will be able to adjust
the viewing angle with your mouse. The PythonPlot
backend relies on matplotlib, the
well-known Python graphics library. Doing
using Plots
pythonplot()
changes the backend to PythonPlot, which also supports
mouse interaction for 3D plots.
In this lesson, we have seen how the Plots package is
used to produce a
The Plots
documentation includes a gallery of
examples that you can look through if you are not sure how to go about
creating a particular kind of plot or achieving a particular effect. The
different backends are
also discussed.