Formatting plots with Makie

With this announcement of a Makie update, I finally pushed myself to try it out and must say, I really love it. Both, the plotting script and the output look beautiful and, finally, I am ready to move away from PyPlot, which I like, but I found it tedious to set up (constantly having problems linking it to python) and plotting was sometimes very slow.

Anyway, I played around, and produced the following script/plot for a start:

using CairoMakie
using AbstractPlotting

AbstractPlotting.inline!(false)
x = collect(0:0.1:99.9)
y1 = cumsum(randn(1000))
y2 = cumsum(randn(1000))
y3 = cumsum(randn(1000))


fig, ax, T1 = lines(x, y1,
    figure = (resolution = (1000, 600),),
    axis = (xlabel = "distance / \$10^{-9}\\,\$m", ylabel = "Temperature / Ā°C",
    xgridwidth = 2, xgridstyle = :dash, ygridwidth = 2, ygridstyle = :dash,
    xtickalign=1, yticklabelcolor=:red, ytickalign=1), color = :red)
T2 = lines!(x,y2, color = :darkred, linestyle = :dot)
T3 = lines!(x,y3, color = :darkorange1)
leg = Legend(fig, [T1, T2, T3], ["T1", "T2", "T3"],
  padding = (10, 10, 5, 5), tellheight=true, tellwidth=false,
  orientation = :horizontal, halign = :center)
fig[2,1] = leg
xlims!(ax, 0,100)
ylims!(ax, -25, 50)
fig

So far, so good. There are just 3 things (for now), I couldnā€™t really figure out and I hope, you can help me with:

  1. Is it possible to render maths in labels/text fields like in PyPlot or LaTeXStrings?
  2. I couldnā€™t get custom dashes to work. In the original dotted lines, for example, spaces are so tight, the line style is virtually indistinguishable from the solid lines. When I type something like T2.linestyle = [5,3] or T2.linestyle = [5,2,2,2,2,2] for dashed or dashed-dotted lines, both graphs for T2 and T3 disappear. So, I guess, there is an error produced in the linestyle of T2 although I am not getting an error message. What am I not understanding?
  3. I want to be able to adjust the tick range. I tried yticks!(ytickrange=-40:20:70) and yticks!(fig.scene, ytickrange=-40:20:70), but I get an AssertionError: The Scene does not have an axis!. How can I do this?

Thanks a lot for any help/hints!

2 Likes

Thanks for trying out Makie, glad you like it so far!

  1. We donā€™t have mathtext/latex, yet. I read the other day that matplotlib uses a custom tex-like implementation which GR adapted. Maybe we could do something similar in the future because it would boil down to writing this layout algorithm in Julia and calling it for latex strings (the fonts would also have to match). So not yet but you can do some things with unicode, I think your superscript should work but Iā€™m not sure, there are some artificial limitations to unicode sub/superscripts
  2. Thereā€™s a PR in the works right now to fix the linestyles. These were overlooked so far because until recently people were not so interested in ā€œpublication-qualityā€ plots with Makie, which seems to change a bit with the recent improvements. https://github.com/JuliaPlots/AbstractPlotting.jl/pull/593
  3. You tried to use the tickrange function for the old axis on the new MakieLayout axis. I know the docs would benefit from more clarification, but you want to look at this section http://makie.juliaplots.org/stable/makielayout/laxis.html#Modifying-ticks

Let me know if you need more help!

3 Likes

Thanks for the quick reply. Yes, Makie is really great work. I especially love, how easy it is to arrange subplots in a certain way or to place legends outside the plot. Something that was too tedious to bother in PyPlot.

Maths formatting is somewhat possible as you say, but has its limits. For example in distance / 10ā»Ā¹Ā² m can look pretty ugly with some fonts as there is some spacing between the digits of two-digit numbers. Also, there are no half spaces, which I prefer between the value and the unit (or maybe there is, just hard to find/type). But nothing, a little post-processing canā€™t do for final production-ready figures.

And for the rest, Iā€™ll guess, Iā€™ll be patient. It is great to know though, that there is a Julia plotting version that is being worked onā€¦

Thanks, the difficulty of placing a legend outside a plot correctly is one of the reasons I wrote this layout manager in the first place. It should really not be that hard!

1 Like

Oh, and thanks for the tip with the axes ticks. With ax.xticks = ... thatā€™s even simpler than the other approach.

Makie looks nice. Iā€™m still using Plots (normally: PyPlot) due to the support for LaTeX typesetting.

What is the state of LaTeX typesetting with Makie? In your plot, it seems like there is no support for LaTeX??

2 Likes

There is https://github.com/JuliaPlots/MakieTeX.jl, which is currently broken - I really want to incorporate some of those bits natively into Makie at some pointā€¦

5 Likes

until my PR gets merged, you can work with your manual line patterns. Note that AbstractPlotting wants to have cumulative lengths. So try [0; cumsum(your_vector)] instead.

Ah, cool. Rather unusual though. Is that documented somewhere? It definitely should be. Your PR looks really nice, @fabgrei! I am looking forward to the syntax defining a linestyle via strings like "-...".

Iā€™m just curious. Do I always have to construct the legend giving it the line/marker format and labels? Or can the labels be attached to the graphs (in my case T1, T2, and T3) and then the Legend is constructed automatically?

Iā€™m not sure whatā€™s the best way to automate the legend generation. I kind of like the matplotlib way of specifying labels with each plotting function, then calling legend for the axis. There are just many corner cases with legends (plots with multiple colors, markers, etc., plots you donā€™t want in the legend) and so far it was easier to just leave it explicit.

You can checkout https://github.com/JuliaPlots/AlgebraOfGraphics.jl, a layer ontop of Makie, inspired by ggplot2.

That handles labelling. But itā€™s still a little annoying to customise the looks.

I will have look at AlgebraOfGraphics some point. First, I am more interested in the GeoMakie and GLMakie packages. I like the matplotlib way, too. Or probably itā€™s just what youā€™re used to. Gnuplot does it similar as well. I definitely like the manual settings and the tutorials on showing how to create legends for marker sizes and colours separately or combining markers and lines as one entry in a legend. But, in the end, in 99% of the cases, the matplotlib way would do it in a plain and simple way with labels attached to the data and, hence, less error prone. So it would be great to have this feature eventually, but right now itā€™ll be pretty far down the queue, I suppose.