Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Getting started with Julia

We are going to get started with Julia. This section will describe how to run Julia scripts and interact with the Julia Read-Eval-Print Loop (REPL), the command line or console used to execute Julia code interactively. But first, let's install Julia on your system.

Installing Julia

We are going to install the stable release of Julia on your system. There are many ways to install Julia, depending on your operating system. Here, we are going to install Julia using the official binaries. First, let's download the binary of Julia's Long-Term Support (LTS) release, Julia 1.6. You can install other releases, but I recommend using Julia 1.6 to ensure reproducibility of the book's examples:

  1. Go to https://julialang.org/downloads/.
  2. Download the binary file for the LTS release that matches your operating system (Windows, macOS, Linux, or FreeBSD) and the computer architecture bit widths (64-bit or 32-bit).

If you are a Windows user, we recommend downloading the installer. When choosing the installer, note that a 64-bit version will only work on 64-bit Windows.

If you are using Linux, you should also match the instruction set architecture of your computer processor (x86, ARM, or PowerPC). You can use the following command in your terminal to learn your architecture:

uname --all

Note that i386 on the output means you have a 32-bit x86 processor, and x86_64 indicates a 64-bit x86 processor.

The instructions that follow this step depend on your operating system, so, let's see them separately. Once you have finished, you can test your Julia installation by typing julia -v on your terminal; it should print the installed Julia version.

Linux

Installation on Linux is pretty simple; you should do the following:

  1. Move the downloaded file to the place you want to install Julia.
  2. Decompress the downloaded .tar.gz file. On a terminal, you can use tar -xf followed by the filename to do it.
  3. Put the julia executable that is inside the bin folder of the decompressed folder on PATH. The PATH system variable contains a colon-separated list of paths where your operating system can find executables. Therefore, we need the folder containing the julia executable on that list to allow your operating system to find it. There are different ways to do that depending on the Unix shell that runs in your system terminal. We will give the instructions for Bash; if you are using another shell, please look for the corresponding instructions. You can know the shell that runs in your terminal by executing echo $0.

Using Bash, you can add the full absolute path to the bin folder to PATH in ~/.bash_profile (or ~/.bashrc) by adding the following line to that file:

export PATH="$PATH:/path/to/julia_directory/bin"

You should replace /path/to/julia_directory/bin with the full path to the bin folder containing the julia binary. The $ and : characters are required; it is easy to mess up your Bash terminal by forgetting them.

macOS

To install Julia on macOS, do the following:

  1. Double-click the downloaded .dmg file to decompress it.
  2. Drag the .app file to the Applications folder.
  3. Put julia on PATH to access it from the command line. You can do that by creating a symbolic link to julia on /usr/local/bin – for example, if you are installing Julia 1.6, you would use this:
    ln -s /Applications/Julia-1.6.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia

Windows

If you are on Windows, you should only run the installer and follow the instructions. Please note the address where Julia has been installed. You will need to put the julia executable on PATH to access Julia from the command line. The following instructions are for Windows 10:

  1. Open Control Panel.
  2. Enter System and Security.
  3. Go to System.
  4. Click on Advanced system settings.
  5. Click on the Environment variables... button.
  6. In the User Variables and System Variables sections, look for the Path variable and click that row.
  7. Click the Edit... button of the section that has the Path variable selected.
  8. Click New in the Edit environment variable window.
  9. Paste the path to the julia executable.
  10. Click the OK button.

Interacting with the Julia REPL

Now you have installed Julia, let's start exploring the Julia REPL or console. Let's do a simple arithmetic operation:

  1. Type julia on your system terminal to open the Julia REPL. If you are using Windows, we recommend using a modern terminal as the Windows terminal.
  2. Type 2 + 2 after the julia> prompt on the Julia REPL and press Enter.

Voilà! You have done your first arithmetic operation in Julia.

  1. Exit the Julia REPL using one of the following options:
    • Type exit() and press Enter.
    • Press the control key (Ctrl) together with the D key on a blank line.

Now that we know the basics of interacting with the Julia REPL, let's see some of its more exciting capabilities – tab completion, support for Unicode characters, and REPL modes.

Getting help from tab completion

The Tab key is handy when working on the Julia REPL. It can speed up your code input and show you the available options in a given context. When entering a name on a current workspace, pressing the Tab key will autocomplete the name until there are no ambiguities. If there are multiple options, Julia will list the possibilities.

You can also use tab completion to get information about the expected and keyword arguments that a function can take. To get that, you need to press the Tab key after the open bracket or after a comma in the argument list. For example, you can see the list of methods for the sum function, including possible arguments and their accepted types, by typing sum( and pressing the Tab key. Note that typing sum([1, 2, 3],  and pressing the Tab key will give you a shorter list, as Julia now knows that you want to sum a vector of integers.

Last but not least, you can use tab completion to find out the field names of a given object. For example, let's see the field names of a range object:

  1. Type numbers = 1:10 on the Julia REPL and press Enter. That defines the variable numbers to contain a range of integers.
  2. Type numbers. and press the Tab key. Julia will autocomplete st, as the two fields of a range object start with these two letters.
  3. Press the Tab key. Julia will list the field names of the range object – in this case, start and stop.
  4. Type a to disambiguate and press the Tab key to autocomplete.
  5. Press the Enter key to see the value of numbers.start.

Julia can autocomplete more things, such as the fields of a function output or a dictionary's keys. As we will see next, you can also use tab autocompletion to enter Unicode characters.

Unicode input

Julia allows you to use any Unicode character, including an emoji, on your variables and function names. You can copy and paste those characters from any source. Also, you can insert them using tab completion of their LaTeX-like abbreviation, starting with the \ character. For example, you can enter π by typing \pi and pressing the Tab key.

REPL modes

Julia offers a variety of REPL modes to facilitate different tasks. You can enter the different modes by pressing a specific key just after the julia> prompt. The following figure shows you the built-in REPL modes that come with Julia:

Figure 1.1 – The REPL modes

Figure 1.1 – The REPL modes

Let's test that by using the most helpful mode, the help mode to access the documentation of the split function:

  1. Press the ? key just after the julia> prompt; you will see that the prompt changes to help?>.
  2. Type split and press Enter; you will see the function's documentation and automatically return to the julia> prompt.

Going back to the julia> prompt only requires pressing the Backspace key just after the prompt of the REPL mode. Another way to come back to the julia> prompt is by pressing the control (Ctrl) and C keys together. In this case, we didn't need the Backspace key to return to the julia> prompt as the help mode isn't sticky. However, the shell and pkg modes shown in Figure 1.1 are sticky and require pressing the Backspace key to go out of them.

Running Julia scripts

Using the Julia REPL is helpful for interactive tasks and ephemeral code, but creating and running Julia scripts can be a better option in other situations. A Julia script is simply a text file with the jl extension containing Julia code. The easiest way to run a Julia script in your system terminal is by running the julia executable and giving the path to the script file as the first positional argument. For instance, if you want to run a Julia script named script.jl that is in the current folder, you can run the following line in the terminal:

julia script.jl

We have now installed Julia and learned how to run Julia code. In the next section, we will learn how to install Julia packages and manage project environments to ensure reproducibility.

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