# Has anyone made scatter plots with hexagonal binning?

**URL:** <https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [August 5, 2018, 4:50am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909 "2018-08-05T04:50:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 4:50am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/1 "2018-08-05T04:50:45Z")

</div>

Something came up in my research where I’m doing a scatter plot with way too many points.

Has anyone had experience making hexagonally binned plots like the ones seen here:

> **[Hexagonal Binning: un nuovo metodo di visualizzazione nell’analisi dei dati –...](https://www.meccanismocomplesso.org/hexagonal-binning/)**

// preferably with pgfplots (or plots.jl), but beggars can’t be choosers

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 5, 2018, 5:06am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/2 "2018-08-05T05:06:34Z")

</div>

Doesn’t Plots.jl have a hexbin function?

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 5:26am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/3 "2018-08-05T05:26:58Z")

</div>

Looks like it’s only supported for PyPlots, though.

[https://github.com/JuliaPlots/Plots.jl/search?q=hexbin&type=Code](https://github.com/JuliaPlots/Plots.jl/search?q=hexbin&type=Code)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 5, 2018, 9:29am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/4 "2018-08-05T09:29:18Z")

</div>

I started rolling my own hexbin plotting function. It’s not super fast, but it works:

![36%20AM](https://global.discourse-cdn.com/julialang/original/3X/5/2/528aabe4c65d5d2a6973da250e50efc19e53bd3f.png)

* * *

- `cell 1`

```julia
using GeometricalPredicates

xmin = -15
xmax = 30
ymin = -80
ymax = 170

num_hexagons = 25

dx = xmax - xmin
dy = ymax - ymin

aspect_ratio = (3/4) * dx/dy
width = ( xmax - xmin ) / num_hexagons

xx = [-1/2 , 0 , 1/2 , 1/2 , 0 , -1/2 , -1/2] * width
yy = [1/4 , 1/2 , 1/4, -1/4, -1/2, -1/4 , 1/4] * width / aspect_ratio

cur_hexagons = Array{Any}(num_hexagons+1, num_hexagons+1)

for cur_row in 0:num_hexagons
    x_offset = isodd(cur_row) ? (dx/num_hexagons) / 2 : 0 
    for cur_col in 0:num_hexagons
        tmp_xx = xx - dx/2 - xmin/2 + dx/num_hexagons*cur_col + x_offset
        tmp_yy = yy - dy/2 - ymin/2 + dy/num_hexagons*cur_row
        
        cur_hexagons[cur_row+1, cur_col+1] = Polygon(map(cur_zip -> Point(cur_zip...), collect(zip(tmp_xx,tmp_yy))[1:end-1])...)
    end
end

```

* * *

- `cell 2`

```julia
aa = linspace(xmin, xmax, 200)
bb = sin.(2*2*pi*aa/dx) * dy/2 + (dy/2 + ymin)

cur_info = Array{Any}(num_hexagons+1, num_hexagons+1)

cur_points = map(cur_zip -> Point(cur_zip...), collect(zip(aa,bb)))

for cur_row in 0:num_hexagons
    for cur_col in 0:num_hexagons
        cur_hexagon = cur_hexagons[cur_row+1, cur_col+1]
        cur_info[cur_row+1, cur_col+1] = count(cur_point -> inpolygon(cur_hexagon, cur_point), cur_points)
    end
end

```

* * *

- `cell 3`

```julia
plot()
for cur_row in 0:num_hexagons
    for cur_col in 0:num_hexagons
        cur_hexagon = cur_hexagons[cur_row+1, cur_col+1]
        tmp_xx = map(cur_vertex -> cur_vertex._x, cur_hexagon._p)
        tmp_yy = map(cur_vertex -> cur_vertex._y, cur_hexagon._p)
        
        push!(tmp_xx, cur_hexagon._p[1]._x)
        push!(tmp_yy, cur_hexagon._p[1]._y)
        
        tmp_cc = cur_info[cur_row+1, cur_col+1]
        
        iszero(tmp_cc) && continue
        plot!(tmp_xx, tmp_yy,aspect_ratio=aspect_ratio, label="", fill=true, fillopacity=0.2, color=tmp_cc)
    end
end
# scatter!(aa,bb, label="")

xlims!(xmin, xmax)
ylims!(ymin, ymax)

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [August 5, 2018, 8:04pm UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/5 "2018-08-05T20:04:50Z")

</div>

Here’s something I put together a while back: [GitHub - RalphAS/HexBinPlots.jl: hexbins for the rest of the Julians](https://github.com/RalphAS/HexBinPlots.jl). It’s also rather slow, and last time I tried it didn’t work with the pgfplots backend, but it did what I wanted at the time.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 6, 2018, 11:01pm UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/6 "2018-08-06T23:01:24Z")

</div>

It should definitely work with at least GR as well

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 7, 2018, 12:20am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/7 "2018-08-07T00:20:33Z")

</div>

> [@mkborregaard](#):
>
> It should definitely work with at least GR as well

Can you provide an example of this?

// I get a backend not supported message (if I’m doing it the correct way?)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [August 7, 2018, 8:01am UTC](https://discourse.julialang.org/t/has-anyone-made-scatter-plots-with-hexagonal-binning/12909/8 "2018-08-07T08:01:59Z")

</div>

No I just now that GR has native hexbins. If it doesn’t works out of the box could you raise an issue?
