Julia language: a concise tutorial
  • Introduction
  • Language core
    • 1 - Getting started
    • 2 - Data types
    • 3 - Control flow
    • 4 - Functions
    • 5 - Custom structures
    • 6 - Input - Output
    • 7 - Managing run-time errors (exceptions)
    • 8 - Interfacing Julia with other languages
    • 9 - Metaprogramming
    • 10 - Performance (parallelisation, debugging, profiling..)
    • 11 - Developing Julia packages
  • Useful packages
    • Plotting
    • DataFrames
    • JuMP
    • SymPy
    • Weave
    • LAJuliaUtils
    • IndexedTables
    • Pipe
Powered by GitBook
On this page
  • Plotting multiple groups of series from a DataFrame
  • Printing area charts
  • Saving
  1. Useful packages

Plotting

Previous11 - Developing Julia packagesNextDataFrames

Last updated 5 years ago

Plotting in julia can be obtained using a specific plotting package (e.g. , ) or, as I prefer, use the package that provide a unified API to several supported backends

Backends are chosen running chosenbackend() (that is, the name of the corresponding backend package, but written all in lower case) before calling the plot function. You need to install at least one backend before being able to use the Plots package. My preferred one is (a julia interface to the visualization library. ), but you may be interested also in (that use the excellent python VERSION 2).

For example:

Pkg.add("Plots")
Pkg.add("PyPlot.jl") # or  Pkg.add("PlotlyJS")
using Plots
pyplot()             # or plotlyjs()
plot(sin, -2pi, pi, label="sine function")

Attention not to mix using different plotting packages (e.g.Plots and one of its backends). I had troubles with that. If you have already imported a plot package and you want to use an other package, always restart the julia kernel (this is not necessary, and it is one of the advantage, when switching between different bakends of the Plots package).

You can find some useful documentation on Plots backends:

Plotting multiple groups of series from a DataFrame

using DataFrames, Plots, StatsPlots
df = DataFrame(
  fruit       = ["orange","orange","orange","orange","apple","apple","apple","apple"],
  year        = [2010,2011,2012,2013,2010,2011,2012,2013],
  production  = [120,150,170,160,100,130,165,158],
  consumption = [70,90,100,95,   80,95,110,120]
)
pyplot()
mycolours = [:green :orange] # note that the serie is piled up alphabetically
@df df plot(:year, :production, group=:fruit, linestyle = :solid, linewidth=3, label = reshape(("Production of " .* sort(unique(:fruit))),(1,:)), color=mycolours)
@df df plot!(:year, :consumption, group=:fruit, linestyle = :dot, linewidth=3, label = reshape(("Consumption of " .* sort(unique(:fruit))),(1,:)), color=mycolours)

The first call to plot() create a new plot. Calling plot!() modify instead the plot that is passed as first argument (if none, the latest plot is modified)

Printing area charts

Use the fill(fillrange,fillalpha,fillcolor) attribute, e.g. fill = (0, 0.5, :blue).

Saving

To save the figure just call one of the following:

savefig("fruits_plot.svg")
savefig("fruits_plot.pdf")
savefig("fruits_plot.png")

The following example uses in order to work directly on DataFrames (rather than on arrays). Passing the dataFrame as first argument of the @df macro, you can access its columns by name and split the overall series using a third column.

While an updated, expanded and revised version of this chapter is available in "Chapter 9 - Working with Data" of , this tutorial remains in active development.

Which backend to choose ?
Charts and attributes supported by the various backends
StatsPlots
Antonello Lobianco (2019), "Julia Quick Syntax Reference", Apress
Gadfly
Winston
Plots
PlotlyJS
plotly.js
PyPlot
matplotlib
sine_plot
fruits_plot