# CairoMakie get 3d plots to respect z order

**URL:** <https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [February 19, 2024, 10:47pm UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428 "2024-02-19T22:47:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [February 19, 2024, 10:47pm UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/1 "2024-02-19T22:47:56Z")

</div>

I’m trying to export figure as a PDF using CairoMakie. Some of my subplots are 3d axes. I’m plotting several surfaces that wrap around each other, so some parts of surface A are behind surface B and some parts are in front. I’m looking for any way of exporting my whole figure as a vectorised pdf using CairoMakie but somehow keeping the 3d plots correct.

For some reason I thought rasterizing the plot objects might help, and I thought there might be a way to “rasterize multiple plot elements together”. Is that possible? Is there any workaround? And I take it there’s definitely no workaround to get vectorised 3d plots working?

edit: They don’t need to be surfaces, lines would be fine. I just checked with MATLAB, and after importing a .pdf from matlab into inkscape it seems like they split their line object up into lots of small lines so different parts of line A can be infront/behind different parts of line B? If I wanted to implement this can I just loop over all the lines in my axis, split every line up into N lines, calculate each sub-line’s distance from the camera and then replot all the lines in the correct order? Is that all it would take to get vectorised lines in a 3d axis or am I missing something?

edit2: I did the above and it seems to work well so far. apologies for making a post without trying a bit harder…

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 20, 2024, 1:10am UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/2 "2024-02-20T01:10:40Z")

</div>

Maybe this helps? [CairoMakie z-order](https://docs.makie.org/stable/explanations/backends/cairomakie/index.html#z-order)

---

<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:** [February 20, 2024, 10:34am UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/3 "2024-02-20T10:34:43Z")

</div>

Surfaces won’t work in vector graphics unless you manually computed their intersections and resolved the depth order. Sounds pretty complicated. One alternative with rasterization would be to use GLMakie and then copy the rasterized part over via the `colorbuffer` function.

---

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [February 21, 2024, 5:38pm UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/4 "2024-02-21T17:38:28Z")

</div>

For now I’ve written some code that splits all the lines and surfaces in an Axis up into sub-lines and sub-surfaces and replots them all in the correct order. This works quite well for simple cases

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/5/653a95f781e671e070399b1ab8f0fb7f38359bc6.png)

But for more complex surfaces (and my actual use case) there are some small lines where the sub-surfaces meet which I can’t quite figure how to get rid of  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/782173363784c44e6854553a50bbc1bfdf4b1e7c.png)

which doesn’t really matter I suppose, and by rasterizing every sub-surface the file sizes aren’t too large.

I might look into `colorbuffer` if I decide that the small artefacts where surfaces meet is too annoying, thanks for pointing that out!

---

<div class="post-metadata">

**Author:** ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)\
**Post date:** [February 21, 2024, 5:45pm UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/5 "2024-02-21T17:45:24Z")

</div>

This is slightly tangentially related but I wanted to point out that I tackled a related issue about a year ago but with scatter. I eventually solved the problem using guidance from others here by sorting my points from furthest to closest to the camera. Maybe some details there will be useful to you.

> [@Point Occlusion in 3D Scatter Makie](https://discourse.julialang.org/t/point-occlusion-in-3d-scatter-makie/95413):
>
> When I create a 3D scatter plot in Makie some of the points that are “closer” to the camera are plotted behind points that are further from the camera. This can make the plots harder to interpret. Is there a setting or technique to change or improve this behavior? The below image shows what I mean where some points that are higher on the z-axis and therefore closer to the camera (lighter color)are plotted behind points that are lower down and further from the camera.

---

<div class="post-metadata">

**Author:** ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)\
**Post date:** [February 21, 2024, 6:09pm UTC](https://discourse.julialang.org/t/cairomakie-get-3d-plots-to-respect-z-order/110428/6 "2024-02-21T18:09:59Z")

</div>

Hi thanks for your reply, definitely very related! This is basically what I did after splitting up lines and surfaces into sub-lines and sub-surfaces. I then sort the sub-lines and sub-surfaces based on distance from the camera

In case it’s useful for anyone else, here is the code I’m using. Package exports `replot_lines!(ax,Nlines)` and `replot_surfaces!(ax,Nx,Ny,sub_surface_Nx,sub_surface_Ny)`. How well it works requires tuning `Nlines` which is how many lines to split the lines into, and `Nx,Ny,sub_surface_Nx,sub_surface_Ny`, which are parameters for how many points to split up the original surface into and how many points to calculate for the sub-surfaces.

I expect it won’t work for lots of cases, but works okay for me!

> **[GitHub - pfuchter/CairoMakieZReorder](https://github.com/pfuchter/CairoMakieZReorder)**
>
> Contribute to pfuchter/CairoMakieZReorder development by creating an account on GitHub.
