Skip to main content

Chapter 15 The Plots Package

There is a relatively simple, but powerful plotting package called Plots and don’t forget to download it as in Appendix B. The full documentation is at the Plots.jl website. Recall that once the package is added, enter
 1 
This function often takes a while to load due to some precompiling. Also, the first time a plot is created, it’ll take a while. The julia team knows this is a problem and calls is the TTFP or time to first plot problem.
The Plots package tries to unify the syntax for plotting anything. The basic command for plotting data or functions in 2D is the plot command. The idea call plot on any type of object that can be plotted. The next few examples shows this.

Section 15.1 Plotting Functions

For plotting a function, simply call plot on the function:
			plot(x->x^2)
			
produces the following plot: \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot01.tex}{plots} \end{center}
Note: your plot may look a bit different than this one with different fonts. This is mainly due to using a different backend, which is explained below.
If you want to specify the \(x\)-range, try: plot(x->x^2,-2,2) which generates:
\begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot01a.tex}{plots} \end{center}
If we want to plot 2 or more functions on the same axes, pass in an array of functions like:
			plot([x->x^2,x->sin(x)],-2,2)
			
produces the following: \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot02.tex}{plots} \end{center}
We will also see below how to change other aspects of the plot including the legend, title, labels on the axes, etc.

Section 15.2 Other Function Plots

Subsection 15.2.1 Parametric Plots

To do a parametric plot, like the circle defined by \(x(t)=\cos t, y(t)=\sin t\text{,}\) then
				plot(t->cos(t),t->sin(t),0,2*pi,legend=false)
				
where the legend is turned off, since with one curve, it doesn’t make much sense. The result is \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/parametric01.tex}{plots} \end{center}
but notice that this should be a circle, but it looks like an ellipse due to the aspect ratio. If one instead adds the \verb!aspect_ratio=:equal! option, as in
				plot(t->cos(t),t->sin(t),0,2*pi,aspect_ratio=:equal, legend=false)
				
\begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/parametric02.tex}{plots} \end{center}

Subsection 15.2.2 Implicit Curves

An implicit curve is the set of points such that \(f(x,y)=0\) (or any constant) and a circle is the classic example. The curve below is the set of all points such that \(x^2+y^2=1\text{.}\) It can be plotted in Julia with the following command.
				contour(-1.1:0.01:1.1, -1.1:0.01:1.1, (x,y) -> x^2+y^2, levels=[1], aspect_ratio = :equal, legend = false)
				
\begin{center} \pgfplotsset{scale=0.7} \IfFileExists{./plots/plots/impcurve_preamble.tex}{\input{plots/plots/impcurve_preamble.tex}}{NO IMPLICIT CURVE PREAMBLE} \plot{plots/plots/implcurve.tex}{plots} \end{center}
Note that again, we have used the \verb!aspect_ratio = :equal! to ensure that the circle looks like a circle. The resulting plot is exactly the same as the circle above.
Here’s a more interesting contour plot of the function \(f(x,y) = \sin (x+y) -\cos(xy) +1\) and following is the implicit curve when \(c=0\text{.}\)
				contour(-10.1:0.1:10.1, -10.1:0.1:10.1, (x,y) -> sin(x+y)-cos(x*y)+1, levels=[0], aspect_ratio = :equal, colorbar_entry = false)
				
\begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/implcurve2.tex}{plots} \end{center}

Subsection 15.2.3 Contour Plots

We can extend this notion to any function of two variables, say \(f(x,y)\) and plot many implicit curves together for different constants. That is plots of the form \(f(x,y)=c\) for multiple \(c\) values and this is called a contour plot. The following is \(x^2+y^2=c\) for \(c=1,4,9,16\) which creates 4 concentric circles.
				contour(-5.1:0.1:5.1, -5.1:0.1:5.1, (x,y) -> x^2+y^2, levels=[1,4,9,16], aspect_ratio = :equal, colorbar_entry = false)
				
\begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/contour1.tex}{plots} \end{center}

Subsection 15.2.4 Surface Plots

If we have a function of 2 variables, a surface plot is nice to use. For example, if we have the function \(f(x,y)=e^{-(x^2+y^2)/10}\) and we want to plot it from -3 to 3 in both directions, if we define
				f(x,y)=exp(-0.1*(x^2+y^2))
				x = y = range(-5, stop = 5, length = 40)
				
and then plot with
				surface(x,y, f, legend = false)
				
\begin{center} \pgfplotsset{scale=0.7} \IfFileExists{./plots/plots/surf_preamble.tex}{\input{plots/plots/surf_preamble.tex}}{} \plot{plots/plots/surf.tex}{plots} \end{center}

Subsection 15.2.5 Heat maps

Similar to above, we can make a heat map with
				pl = heatmap(x,y, f, st=:heatmap)
				
which produces \begin{center} \pgfplotsset{scale=0.7} \IfFileExists{./plots/plots/heat_preamble.tex}{\input{plots/plots/heat_preamble.tex}}{} %\plot{plots/plots/heat.tex}{plots} {\color{red} This plot isn’t working.} \end{center}

Section 15.3 Plotting Data

First, let’s start with some random data. Let
				x=1:10
				y=rand(1:10,10)
				
then plot(x,y) will produce a scatter plot of the data, like \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot03.tex}{plots} \end{center}
and note that since these are just random points, your plot will look different, but the style should be the same.
If we want to connect all of the points with points instead, type
				plot(x,y,seriestype=:scatter)
				
and the plot will look like: \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot04.tex}{plots} \end{center}
There is a shorthand or different version of this: \jlv[plots]{scatter(x,y)}, which produces the same plot. If you want both, then type
				plot(x,y,seriestype=[:scatter,:line])
				
\begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/plot05.tex}{plots} \end{center}

Subsection 15.3.1 Bar plots

A bar plot can be made with the bar command. For example:
				bar(x,y)
				
where x and y were defined above for the scatter plot. The result is \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/bar01.tex}{plots} \end{center}

Subsection 15.3.2 Animation

Another nice type of plot under Plots.jl is that of an animation, however you will need to have ffmpeg installed on your machine. If you then do
				@gif for a in range(0.5,stop=2,length=16)
				plot(t->cos(2t),t->sin(a*t),0,2pi,legend=false)
				end
				
which saves to a gif that the output will describe this results in:

Section 15.4 Other Plots and subplots

This just is the tip of the iceberg for plotting. Take a look at the Plots.jl documentation or do some google-foo with the phrase `Plots.jl’ and what you’re looking for and good spot for Q & A is a julialang.org discourse site

Check Your Understanding 15.1.

Use the plotting techniques in this section to plot the following. For each, hide the legend when ther e is only one curve/set of data and label appropriate if more than one curve/set of data. Include a title as well.
(a)
The function \(y=e^{-x^2}\) from \(x=-3\) to \(x=3\)
(b)
The functions \(y=\sin x, y=\sin 2x, y=\sin 3\) from \(x=-\pi\) to \(x=\pi\text{.}\)
(c)
A scatter plot of the following:
Table 15.2. Data for the scatter plot exercise
0 2 3 4 6 9 10 11 13 15 16 18 20
-1 4 3 6 2 0 2 9 5 -2 4 4 6 3
(d)
A surface plot of \(z=\sin(x-y)\) with \(0 \leq x \leq \pi, 0 \leq y \leq \pi\text{.}\)
(e)
A heat map of \(z=\sin(x-y)\) with \(0 \leq x \leq \pi, 0 \leq y \leq \pi\text{.}\)

Section 15.5 Backends of the Plots package

The Plots.jl package actually doesn’t do the plotting. It leaves the details to other packages. By default, Plots uses the GR backend although in this text I have used the PGFPlotsX backend which tends to print nicer. There are a number of backends that you may want to try. The standard ones are:
  • PyPlot (matplotlib): Slow but dependable
  • GR: Feature-rich and fast, but new
  • Plotly/PlotlyJS: Interactive and good for web
  • PGFPlotsX: Native LaTeX rendering
  • UnicodePlots: Plots to unicode for situations without graphics capabilities.
To switch the backend, you type the backend name in all lowercase with a set of (). Note: you will need to add and load the package. Here is the plot of \(x^2\) on various backends.
				gr()
				plot(x->x^2,-2,2)
				
gives: \begin{center} \includegraphics[width=4in]{plots/plots/plot08.png} \end{center}
				using PlotlyJS
				plotlyjs()
				plot(x->x^2,-2,2)
				
results in: \begin{center} \includegraphics[width=4in]{plots/plots/plot09.png} \end{center}
and then
				using PGFPlotsX
				pgfplotsx()
				plot(x->x^2,-2,2)
				
results in: \begin{center} \pgfplotsset{scale=0.6} \plot{plots/plots/pgfplotsx.tex}{plots} \end{center} and finally
				using PyPlot
				pyplot()
				plot(x->x^2,-2,2)
				
results in: \begin{center} \includegraphics[width=4in]{plots/plots/plot11.png} \end{center}
For additional information on the supported backends, visit the Plots.jl backend documentation

Subsection 15.5.1 Changing the attributes of the plot

Let’s return back to the function plots above (although this works for point/line plots as well) and change many attributes of the curve. As an example:
				plot([x->x^2,sin],-2,2,title="Two Curves",label=["x squared" "sin(x)"],xlabel="x",ylabel="y",lw=3)
				
results in \begin{center} \pgfplotsset{scale=0.7} \plot{plots/plots/attrs.tex}{plots} \end{center}
The format is fairly clear for the changing of the attributes. Note: in this example:
  • the title changes the title of the plot
  • the label changes the legend.
  • the xlabel and ylabel changes the axes labels.
  • the lw is the line weight.
To avoid duplicating tons of documentation, visit the Plots.jl page on attributes to find all of the information to get the plot the way you want.
Check Your Understanding 15.3.
Answer the following questions:
(a)
Take the scatterplot above (with the random dots) and change the color of the dots to darkgreen, change the markers to diamonds and the size of the points to about twice the default size.
(b)
On the function plot, make the line thicker and style to dashed.