# Cartesian product legend

**URL:** <https://discourse.julialang.org/t/cartesian-product-legend/62370>\
**Category:** General Usage\
**Tags:** makie\
**Created:** [June 4, 2021, 12:34am UTC](https://discourse.julialang.org/t/cartesian-product-legend/62370 "2021-06-04T00:34:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 4, 2021, 12:34am UTC](https://discourse.julialang.org/t/cartesian-product-legend/62370/1 "2021-06-04T00:34:51Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5fbdc8e9ff2b250c76ccd170d4238f41bc0d096.png)

Is it possible to have legend with

```julia
      red green blue orange
05     
10
15
20

```

with sized colored images in each cell and row/column labels?

```julia

f = Figure()

Axis(f[1, 1])

markersizes = [5, 10, 15, 20]
colors = [:red, :green, :blue, :orange]

for ms in markersizes, color in colors
    scatter!(randn(5, 2), markersize = ms, color = color)
end

group_size = [MarkerElement(marker = :circle, color = :black,
    strokecolor = :transparent,
    markersize = ms) for ms in markersizes]

group_color = [PolyElement(color = color, strokecolor = :transparent)
    for color in colors]

legend = Legend(f[2,1],
[MarkerElement(marker=:circle, markersize=size, color=color, strokecolor=:transparent)
 for size in markersizes for color in colors])

f

```

ERROR: LoadError: MethodError: no method matching layoutable(::Type{Legend}, ::Figure, ::Array{MarkerElement,1})

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2021, 1:48am UTC](https://discourse.julialang.org/t/cartesian-product-legend/62370/2 "2021-06-04T01:48:46Z")

</div>

I guess the answer is that that is not implemented, but you could as a workaround create  
an inset plot without tick marks and labels, and put your axis labels and dots to mimic that.

---

<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:** [June 4, 2021, 11:24am UTC](https://discourse.julialang.org/t/cartesian-product-legend/62370/3 "2021-06-04T11:24:52Z")

</div>

You can kind of recreate some of the legend code. Make a gridlayout, put labels in it, add boxes in which you plot legend elements. The empty `Attributes()` is usually populated using the legend theme, but if you pass all attributes yourself you don’t need it.

```julia
using CairoMakie

f = Figure()

Axis(f[1, 1])

gl = f[1, 2] = GridLayout(tellheight = false)
for (i, label) in pairs(["a", "b", "c"])
    gl[1, i+1] = Label(f, label)
end

for (i, label) in pairs(["x", "y", "z"])
    gl[i+1, 1] = Label(f, label)
end

for i in 2:4, j in 2:4
    
    b = gl[i, j] = Box(f,
        color = :transparent,
        width = 30,
        height = 30,
        strokecolor = :transparent)

    MakieLayout.legendelement_plots!(
        f.scene,
        MarkerElement(
            color = rand(RGBf0),
            markersize = rand(10:20),
            marker = :circle,
            strokewidth = 0,
            strokecolor = :black,
            markerpoints = [Point2f0(0.5, 0.5)]
        ),
        b.layoutobservables.computedbbox,
        Attributes()
    )
end

f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/8/4/847dbf4a68b1816aa521254e94c3ba17b3939eb5.png)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 4, 2021, 8:07pm UTC](https://discourse.julialang.org/t/cartesian-product-legend/62370/4 "2021-06-04T20:07:54Z")

</div>

Thanks, that helped.

```julia
using CairoMakie

f = Figure()

Axis(f[1, 1])

markersizes = [5, 10, 15,]
colors = [:red, :green, :blue, :orange]

for ms in markersizes, color in colors
    scatter!(randn(5, 2), markersize = ms, color = color)
end

gl = f[2,1] = GridLayout(tellwidth = false)

gl[1,2:6] = Label(f, "Color",)
for (i, label) in pairs(string.(colors))
    gl[2, i+2] = Label(f, label)
end

gl[2:6,1] = Label(f, "Size")
for (i, label) in pairs(string.(markersizes))
    gl[i+2, 2] = Label(f, label,)
end

for (ic, c) in enumerate(colors), (im, m) in enumerate(markersizes)

    b = gl[im+2, ic+2] = Box(f,
        color = :transparent,
        width = 30,
        height = 30,
        strokecolor = :transparent)

    MakieLayout.legendelement_plots!(
        f.scene,
        MarkerElement(
            color = c,
            markersize = m,
            marker = :circle,
            strokewidth = 0,
            strokecolor = :transparent,
            markerpoints = [Point2f0(0.5, 0.5)]
        ),
        b.layoutobservables.computedbbox,
        Attributes()
    )
end

f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/2/323924c61674117a834236e7810dbd51eefe5084.png)
