In this short post, I show one of the many ways of using Julia within emacs org mode, and will describe some of the basic plotting functionality in Julia.
Getting Started with Julia in Org Mode:
While julia-snail is my favorite Julia development environment, it's support for
org-mode is, at present, quite limited. For example, it seems to ignore the
:file argument, making it difficult to save figures to specific locations. I
find that emacs-jupyter provides the most featureful and reliable way to work
with Julia in org-mode. Read the emacs-jupyter documentation for instructions on
how to use emacs-jupyter with org-mode.
Making some Plots
Let's make some plots. We'll use the Plots.jl package and explore a few
different plotting styles. First, we load the packages we'll be using. I tend to
plot statistical distributions fairly often, so I'll load
Statplots in addition to
using Plots using Distributions using StatsPlots
Building Plots Incrementally
With these packages loaded, we'll start simply and plot a standard normal distribution.
Plots.jl package makes it easy to update plots after creation. A Julia
convention is that methods ending in
! modify their arguments in place. In this
case, we can call
plot!() to incrementally add to our plot.
plot!(title="Standard Normal Distribution", xlabel="x", ylabel="p(x)") # Add Labels plot!(leg=false) # Remove the Legend
Let's make one final set of changes and update some of the font sizes for better readability.
plot!(tickfont=font(18, "courier"), guidefont=font(18), titlefont= font(18, "Computer Modern"))
Plotting Real Data
That's enough of that. Usually we're plotting real data, not standard standard
distributions. Let's get some. First we'll pull from the
which is an excellent source of go-to data science and statistics examples such
iris. We'll use the venerable
mtcars to show how to work with data
in a basic way.
using RDatasets, DataFrames mtcars = dataset("datasets", "mtcars") mtcars[1:10,:]
10 rows × 12 columns (omitted printing of 3 columns)
|2||Mazda RX4 Wag||21.0||6||160.0||110||3.9||2.875||17.02||0|
|4||Hornet 4 Drive||21.4||6||258.0||110||3.08||3.215||19.44||1|
We can use the
@df macro and access columns by prepending them with
gr() @df mtcars scatter(:MPG, :HP, group=:Cyl, background=:black, msize=8, keytitle="N Cylinders") xlabel!("Mpg") ylabel!("HP") title!("Horsepower by MPG and N Cylinders")
Modifying Plot Visual Components
It's not always straightforward to figure out how to modify visual components of a plot. Most of the relevant information lies in the Attributes section of the Plots.jl documentation (and its subsections).
Different Plot Styles
One of the benefits of the Plots.jl package is its support for many different plotting backends. The default is GR which, according to the documentation, is
The default backend. Very fast with lots of plot types. Still actively developed and improving daily.
and it offers speed; 2D and 3D plots, and standalone or inline plotting.
Here we'll repeat the plot above with the UnicodePlots backend. We first need to
install the package with
add UnicodePlots in the package manager. Note that you
can do this right from the Jupyter repl in emacs-jupyter by pressing
] in the
We then specify that we'd like to use this backend with the
unicodeplots() # scatterplot @df mtcars scatter(:MPG, :HP, title="HP vs MPG", xlabel="Mpg", ylabel="HP")
HP vs MPG +----------------------------------------+ 343.49 | ⚬ | y1 | | | | | ⚬ | | ⚬ ⚬ | | ⚬ | | ⚬ | HP | | | ⚬ ⚬ ⚬ ⚬⚬⚬ | | ⚬⚬ | | | | ⚬ ⚬ ⚬ ⚬ | | ⚬ ⚬ ⚬ | | ⚬ ⚬ ⚬ | 43.51 | ⚬ ⚬ | +----------------------------------------+ 9.695 Mpg 34.605
I haven't yet figured out how to get the colors to appear as they are supposed to in org mode, so for now, I'm simply showing a basic scatterplot with no grouping. I'll update if and when I figure that out. It should look like this:
There's a lot more to get into with plotting in Julia and with using Julia in emacs. This post serves as a small jumping-off point—just enough to get started, with a few pointers to further resources, and some questions to start pursuing. I'll write more on this topic as I learn more!