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
The data: Anscombe's quartet
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
.
X_1 | Y_1 | X_2 | Y_2 | X_3 | Y_3 | X_4 | Y_4 | |
---|---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
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 |
For each data set, I, II, III, and IV:
Plotting the data
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.)
xxxxxxxxxx
using Plots
xxxxxxxxxx
plotly()
(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 variantscatter(x, y, ...)
to make a scatter plot instead of a line plot.I'll start by putting all datasets on the same plot.
x
begin
scatter(anscombe[!, :X_1], anscombe[!, :Y_1], grid = false, label = "I", border = :box, legend = (0.11, 0.91))
xlabel!("X"); ylabel!("Y");
xlims!(0, 20); ylims!(0, 20);
scatter!(anscombe[!, :X_2], anscombe[!, :Y_2], label = "II");
scatter!(anscombe[!, :X_3], anscombe[!, :Y_3], label = "III");
scatter!(anscombe[!, :X_4], anscombe[!, :Y_4], label = "IV");
end
(Option B) Another way to plot the data would be to make subplots. This is relatively straightforward in Julia using the
layout
agrument to theplot()
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 addlayout = n
to the plot command. Here,n
is the number of plots in the grid.
x
begin
# using Measures
I = scatter(anscombe[!, :X_1], anscombe[!, :Y_1], grid = false, label = "I", color = 1)
xlabel!("X (I)"); ylabel!("Y (I)");
II = scatter(anscombe[!, :X_2], anscombe[!, :Y_2], grid = false, label = "II", color = 2);
xlabel!("X (II)"); ylabel!("Y (II)");
III = scatter(anscombe[!, :X_3], anscombe[!, :Y_3], grid = false, label = "III", color = 3);
xlabel!("X (III)"); ylabel!("Y (III)");
IV = scatter(anscombe[!, :X_4], anscombe[!, :Y_4], grid = false, label = "IV", color = 4);
xlabel!("X (IV)"); ylabel!("Y (IV)");
plot(I, II, III, IV, layout = 4, legend = false, minorticks = 5)
xlims!(0, 20); ylims!(0, 20);
end