# Scatterplot with subplots for points

**URL:** <https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097>\
**Category:** Visualization\
**Created:** [March 27, 2021, 10:56pm UTC](https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097 "2021-03-27T22:56:27Z")\
**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:** [March 27, 2021, 10:56pm UTC](https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097/1 "2021-03-27T22:56:27Z")

</div>

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.

```julia
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=?)

```

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [March 28, 2021, 12:31am UTC](https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097/2 "2021-03-28T00:31:40Z")

</div>

You could put an actual density plot at each location.

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

```

---

<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:** [March 28, 2021, 1:03am UTC](https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097/3 "2021-03-28T01:03:18Z")

</div>

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

```julia
# 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)
end

fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/8/488edadb489c19b8f205c52bce59a759bca289b6.png)

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [March 28, 2021, 2:47am UTC](https://discourse.julialang.org/t/scatterplot-with-subplots-for-points/58097/4 "2021-03-28T02:47:50Z")

</div>

`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](https://github.com/JuliaPlots/AbstractPlotting.jl/blob/master/src/makielayout/lineaxis.jl) which is used [here](https://github.com/JuliaPlots/AbstractPlotting.jl/blob/3b520e9f5dfdc0c760a5797c94d08cd217e15253/src/makielayout/layoutables/axis.jl#L168)?) 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.
