Scatterplot with subplots for points

I have several (x,y) locations, each of which has an array of values: n observations at each (x,y) location.

I want to make a scatterplot of the (x,y) pairs, but instead of a circle at each location, I want a little density plot of the observations. I tried making a recipe but couldn’t figure it out. Here is the attempted usage.

k = 100
n = 10000
data = [(rand(Float64), rand(Float64), rand(n)) for _ in 1:k]
df  = DataFrame([(x, y, v) for (x, y, vs) in data for v in vs])

f = Figure(resolution = (800, 600))
ax = f[1,1]
scatter!(df[:, :1], df[:, :2], marker=?)

You could put an actual density plot at each location.

xs = randn(100)
fig, ax, p = density(xs)
density!(ax, xs .+ 5.0, offset=1.0)

Thanks, that works. Is it possible to get labeled axes in each mini-plot?

# Density and scatterplot at each location.

using CairoMakie
using DataFrames
using AbstractPlotting

k = 10
n = 100
data = [(rand(Float64)*10, rand(Float64)*10, rand(n), rand(n)) for _ in 1:k]
df  = DataFrame([(x, y, v, w) for (x, y, vs, ws) in data for (v, w) in zip(vs, ws)])

fig = Figure()
ax = fig[1,1] = Axis(fig)

for d in groupby(df, :1)
    x = d[1, :1]
    y = d[1, :2]
    vs = d[:, :3]
    ws = d[:, :4]
    density!(vs .+ x, offset=y)
    scatter!(vs .+ x, ws .+ y, markersize=1)


Axis is a layout element so I don’t think you can use that. You might be able to reuse some code from it (maybe LineAxis which is used here?) but I don’t know the implementation well enough to tell you what exactly. Or maybe it’s easier to build your own - an axis is just a bunch of lines/linesegments and text calls.