Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Interactive Visualization and Plotting with Julia

You're reading from   Interactive Visualization and Plotting with Julia Create impressive data visualizations through Julia packages such as Plots, Makie, Gadfly, and more

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781801810517
Length 392 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Diego Javier Zea Diego Javier Zea
Author Profile Icon Diego Javier Zea
Diego Javier Zea
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Section 1 – Getting Started
2. Chapter 1: An Introduction to Julia for Data Visualization and Analysis FREE CHAPTER 3. Chapter 2: The Julia Plotting Ecosystem 4. Chapter 3: Getting Interactive Plots with Julia 5. Chapter 4: Creating Animations 6. Section 2 – Advanced Plot Types
7. Chapter 5: Introducing the Grammar of Graphics 8. Chapter 6: Creating Statistical Plots 9. Chapter 7: Visualizing Graphs 10. Chapter 8: Visualizing Geographically Distributed Data 11. Chapter 9: Plotting Biological Data 12. Section 3 – Mastering Plot Customization
13. Chapter 10: The Anatomy of a Plot 14. Chapter 11: Defining Plot Layouts to Create Figure Panels 15. Chapter 12: Customizing Plot Attributes – Axes, Legends, and Colors 16. Chapter 13: Designing Plot Themes 17. Chapter 14: Designing Your Own Plots – Plot Recipes 18. Other Books You May Enjoy

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 Plots@1.0.0 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.

You have been reading a chapter from
Interactive Visualization and Plotting with Julia
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781801810517
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image