How to create a new axis in a loop using CairoMakie?

I’m trying to create a series of histograms by looping through a data frame and plotting each entry as a histogram. There is 20 different plots, so it would be inconvenient to not use a loop.

In the Makie docs, it shows that you can use hist(f[1,1], X, bins = 100) and create individual plots in loop that way, but then I can’t make any adjustments to the axis.

I tried adding a separate axis call in the loop, and I get a method error:

ERROR: MethodError: no method matching iterate(::Makie.Axis)

    N,cols = size(df)
    ind = CartesianIndices(reshape(1:N,:,5))
   
    fig = Figure(resolution = (1200,900))
    for xi in 1:N
        ax = Makie.Axis(fig[ind[xi][1],ind[xi][2]]; aspect = DataAspect());
        hist(ax, df[xi,6], bins = 100) 
    end
    display(fig)

Any suggestions on how to make adjustments to the axis in a loop?

I’m running Julia v1.9.3 on Windows 10 and using CairoMakie v0.10.12

I haven’t checked anything else in your example but you need the mutating hist! to plot into an existing axis. And I always do these quick grids with Axis(fig[fldmod(i, ncols)...], ...)

1 Like

D’oh! Ok that fixed it. I thought it was an issue with the axis. Thanks!

The method error is confusing admittedly, maybe that can be intercepted earlier with a clear message

For others struggling with grids, I think Jules meant Axis(fig[fldmod1(i, ncols)...], ...)