Align axes to each other with Makie.jl

Hello everyone. I’m having trouble with aligning axes of two plots with different scales. Below is a reproducible example:

begin
    fig = Figure(resolution = (1920, 1080));
    update_theme!(fontsize = 24);
    axis = fig[1, 1] = Axis(fig, title = "First plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:20:80));
    axis2 = fig[1, 2] = Axis(fig, title = "Second plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:125:500));
    violin!(axis, rand((0, 1), 500), rand(0:80, 500), color = "#92a8d1");
    violin!(axis2, rand((0, 1), 500), rand(0:500, 500), color = "#b1cbbb");
    linkyaxes!(axis, axis2);
    fig
end

The code produces this plot, which you can see the y-axes of two plots are aligned but not in the way that I expected. The first plot is compressed in order to match with the y-axis of the second one.


So my question is, is there a way in Makie to align the axes with different scales ? Thank you in advance !

That’s exactly what linkyaxes!(axis, axis2) does, it gives both axes the same y-range. One is just much larger than the other in your example. What is the behavior you would like instead?

Hi, thanks for your replying. Maybe I’m using the wrong functionality. What I want to see is two plots are drawn on the same grid with, for example, 5 ticks each on the y-axis that’re aligned with each other. I tried to set the number of ticks as well as ticks displayed manually but the result is far from what I want. Do you have any idea how to handle case like this ?

Can you show a visual example from some other plot that looks like what you’re after? I’m a bit confused because you have two distributions with very different ranges, so either each has its own y axis range, and they won’t have equal ticks, or you link the axes and achieve the result from above.

Hi, below is the example that I’m trying to recreate in Makie. Please note that in the picture both plots are drawn in a same grid which are aligned with each other, despite as you said they came from a different distributions. Also, the ticks are well scattered across the y-axis as well, which is not the case in my plot. I’m not sure whether it’s even possible to create something like that or not.
Example 2

The ticks are not spaced well because your plots are much wider than the domain you’re sampling from. That’s because the kernel density extends farther than the extrema of the data. But you can set datalimits for that on the violin! call.

Other than that there’s nothing “linked” about this plot, it’s just manually chosen to look nice I assume.

Here’s with datalimits = extrema. Much better but you might want to control the limits exactly if you want a perfect match, right now the extrema are a little bit random given the input data.

begin
    fig = Figure();
    axis = fig[1, 1] = Axis(fig, title = "First plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:20:80));
    axis2 = fig[1, 2] = Axis(fig, title = "Second plot",
                    xticks = (0:1:1, ["0", "1"]),
                    yticks = (0:125:500));
    violin!(axis, rand((0, 1), 500), rand(0:80, 500), color = "#92a8d1", datalimits = extrema);
    violin!(axis2, rand((0, 1), 500), rand(0:500, 500), color = "#b1cbbb", datalimits = extrema);
    fig
end

2 Likes

You’re right, I may have think about how to design my plot again. Thank you so much for your help :heart: