# Draw polygon around scatterplot data?

**URL:** https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729
**Category:** Visualization
**Tags:** polygons
**Created:** [September 13, 2019, 6:06pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729 "2019-09-13T18:06:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [September 13, 2019, 6:06pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/1 "2019-09-13T18:06:50Z")

</div>

Is there any package or combination of packages that would let me draw a polygon around a region in scatterplot data? Even just pieces I could cobble something together from.

The end goal is to use it to filter my points to do analyses on separate regions. I found Clipper.jl that does polygon arithmetic, just need a way to make the polygons that doesn’t make me want to die. I’m hoping I don’t have to go back to Matlab 🙂

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 13, 2019, 6:23pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/2 "2019-09-13T18:23:25Z")

</div>

I think you are looking for a convex hull algorithm. I have not tried it but I think this should work: [https://github.com/JuliaPolyhedra/ConvexHull.jl](https://github.com/JuliaPolyhedra/ConvexHull.jl)

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [September 13, 2019, 6:24pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/3 "2019-09-13T18:24:05Z")

</div>

In Gadfly, you can make [custom statistics](https://github.com/GiovineItalia/Gadfly.jl/issues/894). There are packages for convex hulls here: [https://github.com/JuliaPolyhedra](https://github.com/JuliaPolyhedra)

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [September 13, 2019, 7:03pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/4 "2019-09-13T19:03:02Z")

</div>

Thanks, but I’m looking for something manual. There aren’t necessarily useful features to pick out the regions from the data alone. I need to manually draw a polygon around a visual of the data and then I’ll record the points I chose.

---

<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: [September 14, 2019, 7:26am UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/5 "2019-09-14T07:26:42Z")

</div>

Then check out mouse events in makie

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [September 15, 2019, 4:32pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/6 "2019-09-15T16:32:15Z")

</div>

You could use a combination of keyboard and mouse events to pick points to create a polygon from, some control sequence (maybe Ctrl+M or something) to finish the polygon and mask, and then use (for example) Ctrl + U to unmask. From there, you could create a `lines` plot dependent on an Observable, and just update the observable on click.

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [September 16, 2019, 3:29pm UTC](https://discourse.julialang.org/t/draw-polygon-around-scatterplot-data/28729/7 "2019-09-16T15:29:22Z")

</div>

Thanks to you and mkborregaard! I found this post which I think gives me 90% of what I need: [Makie based data labeling setup](https://discourse.julialang.org/t/makie-based-data-labeling-setup/20459)

I just need to figure out how to have a function wait for input and then return the array… I can get away with just clearing and repopulating a global if need be, but returning from a function would be nicer.
