Plotting

Available Plotting Packages

There are several plotting packages available in Julia. We shall focus only on one here - probably the most widely used plotting package in Julia - Plots.jl:

Other popular plotting packages include Makie (with the AlgebraOfGraphs add-on for statistical analysis plotting) and Gadfly - similar to R’s ggplot, which are left for discovery by the reader.

Plots.jl is a generic front-end for several plotting back-ends. These can also be used directly, but Plots allows a single front-end and you can switch the back-end with a single line of code without having to modify the actual calls to plotting functions. Plots.jl also has a powerful recipe system, which is widely supported. Package authors can define recipes for plots of the data types defined in their packages, which will then automatically generate sensible plots with the normal plot() function.

Back-ends Supported

The following back-ends are supported:

  1. GR (default). Supports all features. Best choice for speed. Not currently interactive, but this feature is in development.
  2. Plotly / PlotlyJS. Best choice for interactivity. The plot is generated in a browser. Plotly requires an internet connection, while PlotlyJS runs locally.
  3. PythonPlot. Julia front-end for PyPlot, the Python front-end for Matplotlib. When installing, this will also install a private Python installation (it will use the same installation as Jupyter).
  4. UnicodePlots. Plots generated with Unicode (text) characters right in the REPL.
  5. Gaston. A Julia front-end for the GnuPlot back-end.
  6. InspectDR
  7. PGFPlotsX. Plotting engine based on TikZ - popular for \(LaTeX\) documents

Installation

The Plots.jl package installed as a normal package via the package manager. This will also automatically install the GR.jl package as a dependency. In order to use the other back-ends, they must first be installed individually and then they must be activated, e.g.:

using Plots # Will use default back-end: GR

plotlyjs() # Switch to PlotlyJS - previously installed
# Plots.PlotlyJSBackend()

gr() # Switch back to GR back-end
# Plots.GRBackend()

General use

For detailed documentation on all the features of Plots.jl, see the manual.

A simple plot is generated with the plot() function. An existing plot can be added to with the plot!() function. If no plot variable is specified, this will add to the last generated plot. There are also mutating functions to add/modify the axis labels, plot title etc:

  • title!()
  • xlabel!()
  • ylabel!()
  • etc.

You can combine multiple plots in one. By default, the layout is a simple grid, but there is the option to specify complex layouts.

You can save the plot to file. Both *.png and *.svg formats are available and selected simply by specifying the extension in the filename.

Two examples:

using Plots

data1 = rand(5)
x = randn(10)
y = randn(10)

plot(
    plot(
        data1,
        label = "some data",
        title="Line plot",
        xlabel="sample",
        ylabel="value",
        linecolor = :red,
        linewidth = 3
    ),
    scatter(
        x, y, 
        title = "Scatter plot",
        label = "samples",
        xlabel="x",
        ylabel="y",
        markercolor = :blue,
        markershape = :diamond
        )
)

savefig("plotsexample.svg")

A plot generated with Plots.jl
data2 = rand(7)
data3 = rand(6)
plot(data2, label="Experiment 2", linestyle=:dashdotdot)
plot!(data3, label= "Experiment 3", linewidth = 2)
title!("Two series in a plot")
xlabel!("Sample number")
ylabel!("Sample size")

savefig("plotsexample2.svg")

Another plot generated with Plots.jl

More Advanced Examples

You can also generate contour and surface plots:

f(x, y) = x*sin(x) - y^2 * cos(y)
x = range(0, 5, length=100)
y = range(0, 3, length=50)
z = @. f(x', y)
contour(z)
savefig("img/plotscontour.svg")
surface(x, y, z)
savefig("img/plotssurface.svg")

Plots.jl contour plot

Plots.jl surface plot

The Plots.jl package has a LOT more features than is shown here. This includes histograms, heatmaps and many more. See the Plots.jl manual for details.

Note

Those paying attention may have seen the @. in the previous example. This means every operator and function call in the line is broadcast automatically. I prefer explicitly broadcasting for clarity, but this is commonly used.

Statistics Plots

The StatsPlots.jl package is an extension of Plots.jl that adds typical statistics plots, like box plots, violin plots, kernel density plots etc. Refer to its manual for more information.

Back to top