Starting Data Visualization with the Julia Language and Jupyter Notebooks | by Alan Jones | Towards Data Science

A while ago I wrote about data visualization using Julia and online Jupyter Notebook environment called JuliaBox. At the time JuliaBox was a free service; unfortunately, that is no longer so — it’s not expensive but it isn’t free.
So here is a new version of that article where you use your own Jupyter Notebook environment (which is free). You’ll need to install Jupyter Notebooks, the Julia language and then configure them so that Jupyter knows about Julia.
If that sounds complicated, don’t worry, it isn’t.
The first thing you need is a Jupyter Notebook. If you have it installed already, great. If not you should follow this article :
(The article has a mild Python focus but since Jupyter requires Python to be installed there is nothing very irrelevant here.)
Julia is a relatively new language for data analysis. It has a high-level syntax and designed to be easy to use and understand. Some have called it the new Python.
Unlike Python, though, it is a compiled language, which means that while it is as easy to write as Python, it runs much faster because it is converted to low-level code that is more easily understood by a computer. This is great if you have to deal with large data sets that require a lot of processing.
Julia is also much less fussy about how a program is laid out than Python.
Julia has all the features that you would expect of a modern programming language but, here, we are going to take a look at Julia’s data visualization capabilities. These are both impressive and easy to use.
Once you’ve installed Julia and Jupyter, you can create Jupyter Notebooks that run Julia code, execute them and export them as HTML. You can save your visualizations as standard png files and save them to include in your documents.
If you want to work through the examples in this article, you can download the data files and the Jupyter notebook to use with your own set up. I’ll put the links at the end of the article.
To install Julia you need to go to https://julialang.org/ and download the appropriate version for your operating system. Installation is straightforward, just follow the instructions.
Once you have installed it there are a couple of things you need to do. So start up Julia and you will see the command prompt in a window like this:
The first thing is to allow Jupyter and Julia to talk to each other. Type this at the prompt and the press <return>:
using Pkg
this tells Julia that you are going to use the package manager. Then type the following and hit <return>:
Pkg.add("IJulia")
IJulia is the package that will provide the connection between Jupyter and Julia. You may need to wait while it downloads and installs.
Once you have installed IJulia you will be able to open a new Notebook in Jupyter using Julia instead of Python.
Now you need to add a couple more packages that we are going to use in the tutorial. Type in these lines:
Pkg.add("Plots")
and
Pkg.add("CSV")
and
Pkg.add("DataFrames")
You’ll be using each of these packages later.
Now you can close Julia by executing:
exit()
or by simply closing the window.
Now you are ready to start! So, fire up a Jupyter Notebook using Julia as the kernel and get going.
Julia, as with most other languages, relies on libraries of code for particular specialist purposes. The one that we are initially interested in is called Plots. This provides us with the capability to create visualizations of data.
So the first piece of code that we need to execute is this:
using Plots
When you type this into a code cell in your notebook and press ctrl/enter to execute it, it tells Julia to load the library that we will use to create our visualizations.
When you execute a cell in a notebook for the first time, it may take a little while to execute. This is because the Julia code is compiled on the fly, the first time you execute. Subsequent code runs are much quicker.
projectcodeed.blogspot.com
I usually put different bits of code into new cells in the notebook. This means that I only have to run the code that I need and not the whole notebook. I suggest that you do the same, so in a new cell I typed in the following code:
x = 1:10; y = rand(10); # These are the plotting data plot(x,y, label="my label")
Running it produced the following graph:
Impressive. Let me explain what’s going on.
x = 1:10; y = rand(10); # These are the plotting data
This bit of code creates two bits of data, one is called x and the other y. x is given the value of a range of numbers from 1 to 10, while y is given a range of 10 pseudo-random numbers (each will have a value between 0 and 1). So, we have the basis of a graph here: an x-axis that ranges from 1 to 10 and y values for each of the points on the x axis.
The next bit is easy.
plot(x,y, label=”my label”)
This code calls a function to plot the graph and all we do is give it the x and y values — and, as an extra, we’ve given it a label, too.
That was easy but, course, we really want to visualize some real data.
I have a couple of tables that I have used for other articles. It’s a set of data about the weather in London, UK, over the last few decades. I derived it from public tables provided by the UK Met Office.
The data records the maximum temperature, minimum temperature, rainfall and hours of sunshine recorded in each month. I have two files, one is the complete data set and the other is for 2018, only. They are in CSV format, such as you might import into a spreadsheet.
To deal with these files we need another library that allows us to read CSV files.
We can see the library referenced in the next chunk of code, i.e. using CSV and the following line actually reads in the data to a variable d, which is a DataFrame.
using CSVd = CSV.read("london2018.csv", DataFrame)
The result of running the code is that we now have a table of data that looks like this:
The data that we have downloaded is formed of a table with 6 columns: Year, Month, Tmax (maximum temperature), Tmin (minimum temperature), Rain (rainfall in millimeters) and Sun (the number of hours of sunshine).
This is a subset of the data (for 2018, only) so the Year column has the same value in all the rows.
So, what we have is the data for each month of 2018. If we wanted to plot the maximum temperature in each month in a bar chart we would do this:
bar(d.Month, d.Tmax)
bar is a function that draws a bar chart (what else?) and we provide the columns for the x and y axes. We do this by using the name of the data table, followed by the name of the column. The two names are separated with a dot.
Here we have the column Month as the x axis and Tmax as the y axis- so we are plotting the maximum recorded temperature for each of the 12 months in the table.
Put this in a new code cell and run it and you will be pleasantly surprised (I hope) to see this chart:
If you wanted to produce a line chart, you do much the same thing but use the function plot
plot(d.Month, d.Tmax)
And, if you wanted to plot both maximum and minimum temperatures on the same graph, you could do this:
plot(d.Month, [d.Tmax, d.Tmin], label=["Tmax","Tmin"])
Note that the two values, d.Tmax and d.Tmin, are grouped together in square brackets and separated by a comma. This is the notation for a vector, or singled dimensional array. Additionally, we have added labels for the lines and these are grouped in the same way. We get a graph like this:
Or how about a scatter chart? A scatter chart is often used to see if a pattern can be detected in the data. Here we plot the maximum temperature against the hours of sunshine. As you might expect there is a pattern: a visible correlation — the more hours of sunshine there are, the higher the temperature.
scatter(d.Tmax, d.Sun)
The data we have doesn’t really lend itself to being depicted as a pie chart, so we are going to generate some random data, again — 5 random numbers.
x = 1:5; y = rand(5); # These are the plotting datapie(x,y)
Now we are going to load in a bit more data:
d2 = CSV.read("londonweather.csv"`, DataFrame)
This is similar to the data table that we have been using but rather bigger as it covers several decades of data rather than just one year. This gives us plenty of rainfall data so that we can see the distribution of the levels of rain that occur in London over a longish period.
histogram(d2.Rain, label="Rainfall")
Here’s the result.
It’s all very well seeing these charts in the Jupyter environment but to be useful, we need to be able to save them so as to use them in our documents.
You can save the chart like this:
histogram(d2.Rain, label="Rainfall") savefig("myhistogram.png")
When this code is run the chart will not be displayed but it will be saved with the file name given.
I hope that was useful — we’ve looked at the basic charts available in Julia Plots. There is a great deal more to Julia than we have seen in this short article and a lot more that you can do with Plots too, but I hope you have found that this introduction has whetted your appetite to find out more.
Right click on the three links below to download the files and then copy them in the directory that Jupyter will use. Then in your file list open plotweatherjulia.ipynb.
The notebook is here: plotweatherjulia.ipynb
and the data files are here: london2018.csv and londonweather.csv
As always, thanks for reading. If you would like to know when I publish new articles, please consider signing up for an email alert here.
If you are not a Medium subscriber, how about signing up so you can read as many articles as you like for $5 a month. Sign up here and I’ll earn a small commision.