# Makie equivalent of matplotlib's \`PolyCollection\`

**URL:** <https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [May 13, 2021, 5:19am UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061 "2021-05-13T05:19:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [May 13, 2021, 5:19am UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061/1 "2021-05-13T05:19:49Z")

</div>

I’m looking to reproduce something like the following in Makie:

```julia
using PyPlot

const PolyCollection = PyPlot.matplotlib.collections.PolyCollection
polygon = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]
# force new axes
ax = PyPlot.axes(label=string(Base.UUID(rand(UInt128))))
ax.set_aspect(1.0)

# quite clumbsy
grid(xs, ys) = collect(reinterpret(reshape, Int, reshape(collect(Iterators.product(xs, ys)), :))')

offsets = grid(1:10, 1:15)

# based off https://github.com/matplotlib/matplotlib/blob/40862a32e86ed3a9a67790dc249da13971f1a37d/lib/matplotlib/axes/_axes.py#L4886-L4892
poly_collection = PolyCollection(
    [polygon],
    offsets = offsets,
    edgecolors = "face",
    facecolors = rand(n, 4),
    transOffset = matplotlib.transforms.AffineDeltaTransform(ax.transData),
)

(xmin, xmax) = extrema(offsets[:, 1])
(ymin, ymax) = extrema(offsets[:, 2])
corners = ((xmin, ymin), (xmax, ymax))
ax.update_datalim(corners)
ax._request_autoscale_view(tight=true)

ax.add_collection(poly_collection, autolim=false)

display(ax.figure)
ax.figure.savefig("poly_collection.svg")

```

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

Extra bonus if the SVG output can use an `href` instead of inlining the polygon path repeatedly.

This is how matplotlib implements e.g. [hexbin](https://matplotlib.org/stable/gallery/statistics/hexbin_demo.html#sphx-glr-gallery-statistics-hexbin-demo-py).

---

<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:** [May 13, 2021, 10:17am UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061/2 "2021-05-13T10:17:42Z")

</div>

This would be one way although I can’t claim the SVG href bonus, that would require much more optimization in CairoMakie. In my approach you also have to shift each polygon where you want it as a whole, which is a bit more work (especially with rotations), but the example is still only a couple lines, so 🤷‍♂️

```julia
using CairoMakie

pol = Point2f0[(0, 0), (0, 1), (1, 0)]

offsets = vec(Point2f0.(1:10, (1:15)'))

poly([pol .+ o for o in offsets],
    color = rand(RGBf0, length(offsets)),
    axis = (aspect = DataAspect(),))

```

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

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [May 14, 2021, 1:48pm UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061/3 "2021-05-14T13:48:43Z")

</div>

Thanks! That’s certainly an option.

I need to update the colors of each of the polygons, and I think it would not work well if each were an `Observable`, instead of having one `Observable` array.

---

<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:** [May 16, 2021, 8:01pm UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061/4 "2021-05-16T20:01:50Z")

</div>

I’m not sure I understand. You do want to use an observable with an array of colors? That should already work with the method I showed, no?

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [May 17, 2021, 8:16pm UTC](https://discourse.julialang.org/t/makie-equivalent-of-matplotlibs-polycollection/61061/5 "2021-05-17T20:16:06Z")

</div>

Ah, yes, indeed!
