Book Image

Interactive Visualization and Plotting with Julia

By : Diego Javier Zea
Book Image

Interactive Visualization and Plotting with Julia

By: Diego Javier Zea

Overview of this book

The Julia programming language offers a fresh perspective into the data visualization field. Interactive Visualization and Plotting with Julia begins by introducing the Julia language and the Plots package. The book then gives a quick overview of the Julia plotting ecosystem to help you choose the best library for your task. In particular, you will discover the many ways to create interactive visualizations with its packages. You’ll also leverage Pluto notebooks to gain interactivity and use them intensively through this book. You’ll find out how to create animations, a handy skill for communication and teaching. Then, the book shows how to solve data analysis problems using DataFrames and various plotting packages based on the grammar of graphics. Furthermore, you’ll discover how to create the most common statistical plots for data exploration. Also, you’ll learn to visualize geographically distributed data, graphs and networks, and biological data. Lastly, this book will go deeper into plot customizations with Plots, Makie, and Gadfly—focusing on the former—teaching you to create plot themes, arrange multiple plots into a single figure, and build new plot types. By the end of this Julia book, you’ll be able to create interactive and publication-quality static plots for data analysis and exploration tasks using Julia.
Table of Contents (19 chapters)
1
Section 1 – Getting Started
6
Section 2 – Advanced Plot Types
12
Section 3 – Mastering Plot Customization

Installing and managing packages

Julia has a built-in package manager that you can use by loading the Pkg module or through pkg mode of the Julia REPL. In this section, we will learn how to use it to install packages and manage project environments.

Installing Julia packages

Julia has an increasing number of registered packages that you can easily install using the built-in package manager. In this section, we will install the Plots library as an example. Let's install Plots using the add command from Pkg mode. This way of installing packages comes in handy when working on the Julia REPL:

  1. Open Julia.
  2. Enter Pkg mode by pressing the ] key just after the julia> prompt.
  3. Type add Plots after the pkg> prompt and press Enter.
  4. Wait for the installation to finish; it can take some time.
  5. Press the Backspace key to return to the julia> prompt.

Great! This has been easy, and you now have the Plots package installed. However, Pkg mode is only available to you when you are in the Julia REPL. But, if you want to install a Julia package from a non-interactive environment (for example, inside a Julia script), you will need to use the add function from the Pkg module. The Pkg module belongs to the Julia Standard Library. Thankfully, you don't need to install the packages of the Standard Library before using them. Let's try adding Plots again but using the Pkg module this time. As we have already installed the latest version of Plots, this will be fast:

  1. Open Julia.
  2. Import the Pkg module by typing import Pkg after the julia> prompt and pressing Enter.
  3. Type Pkg.add("Plots") and press Enter.

In the last example, we used import to load the Pkg module. In the next section, we will learn some different ways in which you can load packages.

Loading packages

We need to load a package to use it within a Julia session. There are two main ways to load packages in Julia. The first one is using the import keyword followed by the module name – for example, import Pkg. As Julia packages export modules of the same name, you can also use the name of a package. When we use import in this way, Julia only brings the package's module into scope. Then, we need to use qualified names to access any function or variable from that module. A qualified name is simply the module name followed by a dot and the object name – for example, Pkg.add.

The second way to load a package is to use the using keyword followed by the module name. Julia will bring the module into scope, as well as all the names that the module exports. Therefore, we do not need to use qualified names to access their functions and variables. For instance, executing using Plots will bring the exported plot function into scope. You can still access unexported functions and variables using their qualified names.

Managing environments

A project environment defines a set of package dependencies, optionally with their versions. The Julia package manager has built-in support for them, and we can use them to create reproducible data analysis pipelines and visualizations. For example, this book's code examples use environments to allow the reproducibility of code through time and across different systems.

Julia defines project environments using two files. The first is the Project.toml file that stores the set of dependencies. The second is the Manifest.toml file that stores the exact version of all the packages and their dependencies. While the former is mandatory for any environment, the latter is optional. Luckily, we do not need to create those files manually, as we can manage the environment's packages through the package manager.

There are multiple options for dealing with project environments. One is to start julia in a given environment by using the --project argument. Usually, we want to create an environment in the folder where we are starting Julia. In those cases, we can use a dot to indicate the current working directory. Let's create an environment in the current folder containing a specific version of the Plots package:

  1. Run julia --project=. in the terminal to open the Julia REPL using the project environment defined in the current working directory.
  2. Press the ] key to enter Pkg mode. You will see the name of the current folder on the prompt. That's Pkg mode telling you that you are in that environment.
  3. Type status and press Enter to see the content of your current environment.
  4. Type add [email protected] to install Plots version 1.0.0 in that environment.
  5. Run the status command in Pkg mode again to check what is in the environment after the previous operation.
  6. Press the Backspace key to return to the Julia prompt.

In the previous example, we have used the --project argument to start julia in a particular environment. If you run julia without indicating a project folder, you will use the default environment corresponding to your Julia version.

You can change between environments using the activate command. It takes the path to the project folder that contains the environment, and if you do not give any path, Julia will start the default environment of your Julia version. For example, executing activate . in Pkg mode will start the environment in the current working directory, and running activate will return the default Julia environment.

When you first activate a non-empty environment on your system, you must install all the required packages. To get all the needed packages, you should run the instantiate command of Pkg mode. For example, instantiation will be necessary if you want to use an environment created on another computer.

While Pkg mode of the Julia REPL is handy when you are working interactively, sometimes you need to manage environments inside a Julia script. In those cases, the Pkg module will be your best friend. So, let's create a Julia script that uses a particular version of Plots. First, create a file named installing_plots.jl with the following content, using any text editor:

import Pkg
Pkg.activate(temp=true)
Pkg.add(Pkg.PackageSpec(name="Plots", version="1.0.0"))
Pkg.status()

In that code, we are using the activate function of the Pkg module with temp=true to create and activate the script environment in a temporary folder. We need to use the PackageSpec type defined on the Pkg module to add a specific package version.

Now, you can run the script executing julia installing_plots.jl on your terminal. You will see that the script creates and activates a new environment in a temporal folder. Then, it installs and precompiles Plots and its dependencies. Finally, it shows that Plots version 1.0.0 was installed in the environment. The script will run a lot faster the second time because the packages are installed and precompiled on your system.

There are other Pkg commands and functions that you will find helpful when managing environments – status, to list the packages on the current project environment, update, and remove. You can see the complete list of Pkg commands in Pkg mode by typing ? and pressing the Enter key just after the pkg> prompt. Optionally, you can see extended help for each command by entering ? and the command name in Pkg mode. If you are using the Pkg module, you can access the documentation of the functions by typing Pkg. and the function name in help mode of the Julia REPL.

Now that we know how to install and manage Julia packages, let's start installing some packages to set up the different development environments we will use for Julia.