How to plot data in Julia

This tutorial will show you how to create a basic scatter (x-y) plot in Julia. You will learn:

  • How to create a scatter plot using the Plotly backend

  • How to adjust axes

  • How to add labels to the axes

  • How to adjust grid lines

Throughout this exercise I will work with Anscombe's quartet. This is sets of X and Y (named I, II, III, and IV) that are all described by exactly the same linear model but – as you'll see – are quite different!

9.4 μs

The data: Anscombe's quartet

1.9 μs

The four data sets will be stored in an object called a dataframe. This functions similarly to a spreadsheet, but is accesed by typing in commands rather than with a mouse. The name of the dataframe will be anscombe.

3.5 μs
X_1Y_1X_2Y_2X_3Y_3X_4Y_4
Float64Float64Float64Float64Float64Float64Float64Float64
1
10.0
8.04
10.0
9.14
10.0
7.46
8.0
6.58
2
8.0
6.95
8.0
8.14
8.0
6.77
8.0
5.76
3
13.0
7.58
13.0
8.74
13.0
12.74
8.0
7.71
4
9.0
8.81
9.0
8.77
9.0
7.11
8.0
8.84
5
11.0
8.33
11.0
9.26
11.0
7.81
8.0
8.47
6
14.0
9.96
14.0
8.1
14.0
8.84
8.0
7.04
7
6.0
7.24
6.0
6.13
6.0
6.08
8.0
5.25
8
4.0
4.26
4.0
3.1
4.0
5.39
11.0
12.5
9
12.0
10.84
12.0
9.13
12.0
8.15
8.0
5.56
10
7.0
4.82
7.0
8.26
7.0
6.42
8.0
7.91
11
5.0
5.68
5.0
4.74
5.0
5.73
8.0
6.89
10.5 s

For each data set, I, II, III, and IV:

y=3.00+0.500x

R2=0.67

2.0 μs

Plotting the data

2.1 μs
  1. The first step in plotting is to tell Julia to load the Plots package and then tell Julia you'd like to use the Plotly utility within that. (There are several plotting utilties, called "backends", available in Julia. Plotly has a few nice features so I suggest that one for class.)

2.1 ms
8.2 s
4.0 s
  1. (Option A) The next step is to use the plot(x, y, ...) function to create the plot. This works for a line plot. Here, I'll use the variant scatter(x, y, ...) to make a scatter plot instead of a line plot.

    I'll start by putting all datasets on the same plot.

8.0 μs
1.5 s
  1. (Option B) Another way to plot the data would be to make subplots. This is relatively straightforward in Julia using the layout agrument to the plot() command. This method does take a bit of extra formatting to make the plot look nice.

    With this method, just save each plot it it's own object and then pass all objects to plot(obj1, obj2, obj3, ...) and add layout = n to the plot command. Here, n is the number of plots in the grid.

6.7 μs
73.5 ms