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. Fix linestyle by greimel · Pull Request #593 · JuliaPlots/AbstractPlotting.jl · GitHub
  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 Axis · Makie.jl

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 GitHub - JuliaPlots/MakieTeX.jl: TeX integration in Makie, 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 GitHub - JuliaPlots/AlgebraOfGraphics.jl: Combine ingredients for a plot, 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.