Makie: High-Level, Beautiful Plots | by Emmett Boudreau | Towards Data Science

Within the Julia ecosystem, there have come to be a lot of fantastic solutions for visualizing data. Despite this, many users often end up using Plots.jl. While Plots.jl is certainly a cool package, it is important to remember that all the package has to offer is three separate ports of different software from other programming languages. These ports are Plot.ly, Matplotlib.pyplot, and GR. By default, Plots.jl uses the GR visualization library, which is a great library — but in a lot of ways very featureless. Furthermore, utilizing ports like this really kills precompile times, and can hinder Julia’s performance by using packages like PyCall. Funny enough, I do actually have a full-length tutorial on the GR back end of Plots.jl if you are interested:
towardsdatascience.com
There are also some other purely Julian solutions that have come out of the woodwork. The greatest example of this in my opinion is the Gadfly package. The Gadfly package has interactivity that is not quite on par with something like Plot.ly, but is certainly sufficient for most cases. On top of that, Gadfly visualizations are easy to work with and just look good! I also wrote a tutorial and overview on using that package that you may check out here if you are interested:
towardsdatascience.com
There is another great visualization library written in Julia called Vegalite.jl. I have met many other scientists who swear that this is the best one in Julia, but my preference still remains with Gadfly.jl. This is because I find Gadfly to be a lot easier to use. Vegalite has a weird methodology that involves using a lot of arrays for various different settings in your plots, rather than arguments. That being said, I will say that Vegalite does have some serious statistical visualizations under the hood compared to the other options on the list. I actually compared all three of the options mentioned in another article that you may check out if you desire here:
towardsdatascience.com
It is pretty awesome to have all of these articles with more information to link.
Another recent addition to the Julia language and its plotting capabilities is the Plot.ly packages official port to the language. I must confess, I have not had an opportunity to try this one out, but I am excited to dip my toes into it, and I might even write an article on it and compare it to the Python implementation!
With all of these graphing libraries in the ecosystem, I really wanted to touch base with all of them in order to both compare all of the available options publicly, and find out which one I want to use. It is great to know a lot of them because I think they all certainly have their own functionality. In this journey of Julian graphing libraries, I am now making my next stop at Makie.jl. Makie.jl is a little unique because there are multiple implementations of the library using different graphics libraries. For example, I decided to use OpenGL, so I am going to be adding MakieGL.jl via Pkg. There is also a webgl version and a Cairo version, WGLMakie.jl and CairoMakie.jl respectively. Let us go ahead and add the package and get started!
notebook
julia > ]pkg > add GLMakie
Since I have never actually used the Makie.jl package, I decided to get a little familiar with some basic plots. Makie.jl follows the typical high-level convention for graphing libraries, so fortunately there are not going to be any Vegalite-esc curve balls here.
using GLMakie
Like Plots.jl, I noticed that Makie also takes a really long time to precompile. This could be because I decided to use OpenGL, but I have my doubts that this is the case. I mean it really took a long time. Not too important, but certainly something that you might want to keep in mind when using the package with your applications. The first thing I plotted was this example pulled straight from the Makie documentation:
points = [Point2f0(cos(t), sin(t)) for t in LinRange(0, 2pi, 20)] colors = 1:20 figure, axis, scatterobject = scatter(points, color = colors, markersize = 15) figure
Looks like an OpenGL driver failed to load! The good news is I happen to know for a fact that Cairo works fine with my Julia installation on my computer, so we will switch over to CairoMakie and HAVE TO PRECOMPILE IT AGAIN.
julia > ]pkg > add CairoMakieusing CairoMakiepoints = [Point2f0(cos(t), sin(t)) for t in LinRange(0, 2pi, 20)]colors = 1:20figure, axis, scatterobject = scatter(points, color = colors, markersize = 15)figure
Yay!
I actually like how this library functions a lot, though it is minimalist visualizations similar to that of something like GR without interactivity, that is pretty much all I need in most situations. The types are mutable, so we can change the data inside of this struct now to produce a different result. Even more awesome, we can add figures to this figure! This actually reminds me a lot of a graphing library made a long time ago. It was really cool because it was meta-programmed and object-oriented, but the project has since been discontinued. I actually wrote about it on Towards Data Science, so if you happen to be interested in that project, there is a lot of very very interesting Julia in it, you can check it out here:
towardsdatascience.com
There are a lot of different parts to that series, so it might be a good bit of reading if you are really interested in the package and the code behind it. Anyway, clearly from my development history I am a huge fan of things being modular with my graphing, and Makie.jl offers exactly that! This is quickly becoming my new favorite graphing library. We can add basically any new figure to our figure by using the ! version of the scatter! method.
circlefunc = ts -> 1.5 .* Point2f0.(cos.(ts), sin.(ts))scatter!(circlefunc, LinRange(0, 2pi, 30), color = :red)figure
Makie.jl also has a rather robust implementation of layouts. They are far easier to understand and better made than the GR counterpart, check it out! We can make a completely empty figure, and add all of the modular portions of our visualization atop it!
fig = Figure(resolution = (700, 500), backgroundcolor = RGBf0(0.98, 0.98, 0.98))ax1 = fig[1, 1] = Axis(fig, title = "Pre Treatment")data1 = randn(50, 2) * [1 2.5; 2.5 1] .+ [10 10]line1 = lines!(ax1, 5..15, x -> x, color = :red, linewidth = 2)scat1 = scatter!(ax1, data1,    color = (:red, 0.3), markersize = 15px, marker = '■')fig.layoutax2, line2 = lines(fig[1, 2], 7..17, x -> -x + 26,    color = :blue, linewidth = 4,    axis = (title = "Post Treatment",))fig
In addition to the previous AWESOME and well implemented features, Makie.jl also has support for animations! I am genuinely so excited, I already know I am going to be using this library all the time now! I am already so happy I tried this out, and animations is just icing a cake that is already so moist it melts in your mouth. In order to create an animated plot, we will also need to add the “ AbstractPlots” package.
julia > ]pkg > add AbstractPlots
And I am going to go ahead and try it out. In order to actually create the animation, we will use the record() method. This will actually output it to a video file. This is a little strange, as I certainly would have preferred a GIF or something — but regardless, it will work.
using GLMakie using AbstractPlotting.Colorsfigure, ax, lineplot = lines(0..10, sin; linewidth=10)n_frames = 30 framerate = 30 hue_iterator = LinRange(0, 360, n_frames)  record(figure, "color_animation.mp4", hue_iterator; framerate = framerate) do hue     lineplot.color = HSV(hue, 1, 0.75) end
Or maybe it won’t work.
I am sure with time and effort, there will certainly be a way to get the proper drivers loaded and actually get my system working with the full power of Makie.jl. Regardless of the animation struggle, I am extremely excited to start using this graphing library consistently! Its modular nature and raw abilities are precisely what I look for in something like this. I am really glad I tried it out, I am now actually incredibly excited about this package. I would highly recommend it. The worst part of this package I would say is probably the segmented nature of the packages, you will need to add quite a few packages with Pkg. Of course, that is not too big of a deal, and I think the abilities of this graphing library are most certainly worth it. Thank you for reading! I certainly recommend trying this library out!