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

**URL:** <https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623>\
**Category:** Visualization\
**Tags:** makie, cairomakie\
**Created:** [February 2, 2024, 3:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623 "2024-02-02T15:00:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![albrightand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albrightand/32/38684_2.png) [@albrightand](https://discourse.julialang.org/u/albrightand)\
**Post date:** [February 2, 2024, 3:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623/1 "2024-02-02T15:00:30Z")

</div>

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)`

```julia
    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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 2, 2024, 3:45pm UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623/2 "2024-02-02T15:45:56Z")

</div>

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)...], ...)`

---

<div class="post-metadata">

**Author:** ![albrightand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albrightand/32/38684_2.png) [@albrightand](https://discourse.julialang.org/u/albrightand)\
**Post date:** [February 2, 2024, 3:59pm UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623/3 "2024-02-02T15:59:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 2, 2024, 4:26pm UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623/4 "2024-02-02T16:26:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [February 4, 2024, 2:59am UTC](https://discourse.julialang.org/t/how-to-create-a-new-axis-in-a-loop-using-cairomakie/109623/5 "2024-02-04T02:59:37Z")

</div>

For others struggling with grids, I think Jules meant `Axis(fig[fldmod` **1** `(i, ncols)...], ...)`
