Data Visualization with Julia and VSCode | by Alan Jones | Towards Data Science

A while ago I wrote about data visualization using Julia and an online environment called JuliaBox. At the time JuliaBox was a free service; unfortunately, it has since been withdrawn. This is a shame as it was a great service.
So here is a new version of that article where you use Microsoft’s VSCode IDE (which is not only free but is also unlikely to go away any time soon). You’ll need to install VSCode, the Julia language and then the VSCode extension for Julia. If that sounds complicated, don’t worry, it isn’t.
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 VSCode, you can run Julia code and save your visualizations as standard png files.
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.
For VSCode the advice is the same except you go to https://code.visualstudio.com and download it from there.
When you are ready start VSCode and load the Julia extension.
You do this by clicking on the the extensions symbol in the leftmost panel (the one that is four rectangles with one slightly offset from the others) and type julia in the search bar.
Now click on install.
That should be all you need to do if you have installed Julia normally. If you done something different, you need to click on the settings icon (the cogwheel) and manually type in the location of julia.exe. You can also find the settings in the File|Preferences|Extensions menu.
Before going any further, I must tell you that the instructions and the code in this article were developed on a Windows 10 laptop. I know of no reason why it should be any different on Linux or on a Mac but I have not tested in those environments.
Before we write any programs, we need to load the libraries that we want to use. You only need to do this once, once you’ve loaded the libraries they are there forever.
Do this by opening a Julia terminal (also known as a REPL- for Read, Evaluate, Print Loop).
You can start the REPL in a number of ways. Find the Command Palette from the View menu and click that or press Ctrl+Shift+P. This will give you a list of commands, one of which is ‘Julia: Start REPL’ (if you can’t see it scroll down or start typing ‘julia’ in the search bar and the commands will show up), so click that and you’ll get a terminal window.
At the Julia prompt in the REPL window, type the following
julia> using Pkg
this tells Julia that you are going to use the package manager. Then type the following and hit <return>:
julia> Pkg.add("Plots")
Now you need to add a couple more packages that we are going to use in the tutorial. Type in these lines:
julia> Pkg.add("CSV")
and
julia> Pkg.add("DataFrames")
You’ll be using each of these packages later.
Now you are ready to start! So, open a new file, call it ‘myPlots.jl’ or something similar but make sure it has the extension ‘.jl’ so VSCode knows what sort of file it is. You should now have two panels open, the file and the REPL. You still want to keep the REPL open as this is where any output from your program will be printed.
Your screen should look something like this:
Julia, as with most other languages, relies on libraries of code for particular specialist purposes and we’ve just downloaded the ones we need. 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
This tells Julia to load the library that we will use to create our visualizations.
Here is a simple program to make sure everything is working ok.
using Plotsx = 1:10; y = rand(10); # These are the plotting data plot(x,y, label="my label")
Running it will produce something like the following graph in a new tab in VSCode (to run the code, find the command in the Command Palette (Ctrl+Alt+P) there will be a number of options but you want to execute the whole file):
That may have taken a little while because when you execute code for the first time, the Julia code is compiled on the fly, which slows things down the first time you run something but makes it really quick the next time. So subsequent code runs are quicker.
Let me explain what’s going on with the code.
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.
using CSVd = CSV.read("D:notebooks/juliaplot/london2018.csv")print(d)
The result of running the code is that we now have a table of data that looks like this:
Of course, you will need to change the path for the file name to whereever you have downloaded it to.
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:
using Plotsusing CSVd = CSV.read("D:notebooks/juliaplot/london2018.csv")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")
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. To create a histogram we run this code:
histogram(d2.Rain, label="Rainfall")
So the whole program looks like this:
using Plotsusing CSVd2 = CSV.read("d:notebooks/juliaplot/londonweather.csv")histogram(d2.Rain, label="Rainfall")
Here’s the result.
It’s all very well seeing these charts in the VSCode 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("d:notebooks/juliaplot/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 links below to download the files and then copy them in the directory that Julia will use. 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.