# How to plot a cube in 3D in Plots.jl

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919>\
**Category:** Visualization\
**Tags:** plots, 3d\
**Created:** [September 8, 2022, 12:46am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919 "2022-09-08T00:46:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)\
**Post date:** [September 8, 2022, 12:46am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/1 "2022-09-08T00:46:15Z")

</div>

I would like to plot a 3D cube in Plots.jl and I have not been able to.  
I know that there are solutions in Makie but I would like to use the Plots.jl interface because I want to combine this with other things plotted with Plots.jl.

So far I can get wireframe working with:

```julia
Plots_jl.plot!(ax, [x, x + w1, x+w1, x, x], [y, y, y+w2, y+w2, y], [z, z, z, z, z], aspect_ratio=1.0) # bottom
    Plots_jl.plot!(ax, [x, x + w1, x+w1, x, x], [y, y, y+w2, y+w2, y], [z+w3, z+w3, z+w3, z+w3, z+w3],) # top
    Plots_jl.plot!(ax, [x, x, x, x, x], [y, y+w2, y+w2, y, y], [z, z, z+w3, z+w3, z]) # back left 
    Plots_jl.plot!(ax, [x, x+w1, x+w1, x, x], [y+w2, y+w2, y+w2, y+w2, y+w2], [z, z, z+w3, z+w3, z]) # back right 
    Plots_jl.plot!(ax, [x, x+w1, x+w1, x, x], [y, y, y, y, y], [z, z, z+w3, z+w3, z]) # front left 
    Plots_jl.plot!(ax, [x+w1, x+w1, x+w1, x+w1, x+w1], [y, y+w2, y+w2, y, y], [z, z, z+w3, z+w3, z])

```

But I have tried to create solid surfaces with:

```julia
# Plots_jl.plotly()
x,y,z = 0.,0.,0.
w1,w2,w3 = 1.0,1.0,1.0
xrange = range(x, stop=x+w1, length=10)
yrange = range(y, stop=y+w2, length=10)
zrange = range(z, stop=z+w3, length=10)
Plots_jl.surface(xrange, yrange, (a1,a2)-> z, xlabel="x", ylabel="y", zlabel="z") # bottom
Plots_jl.surface!(repeat([x], length(xrange)), yrange, (a1,a2)->zrange[findfirst(a -> a == a2, yrange)]) # back left 
# back right 
# front left 
# front right 
Plots_jl.surface!(x:.1:x+w1, y:.1:y+w2, (a1,a2)-> z+w3) # top

```

and it does not work.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 8, 2022, 6:55am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/2 "2022-09-08T06:55:05Z")

</div>

You can not plot vertical surfaces using `surface`, you’d need `mesh3d` for this:

```julia
using Plots
xp = [0, 0, 0, 0, 1, 1, 1, 1];
yp = [0, 1, 0, 1, 0, 0, 1, 1];
zp = [0, 0, 1, 1, 1, 0, 0, 1];
connections = [(1,2,3), (4,2,3), (4,7,8), (7,5,6), (2,4,7), (1,6,2), (2,7,6), (7,8,5), (4,8,5), (4,5,3), (1,6,3), (6,3,5)];

mesh3d(xp,yp,zp; connections, xlabel="x", proj_type = :persp, linecolor=:black)

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 12, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/3 "2023-02-12T19:37:01Z")

</div>

Using your solution, there must be a way to remove these triangulation lines right?

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

Ideally I want to keep the “outer edges”, but I cannot figure out how to do this. The documentation confuses me.

Kind regards

---

<div class="post-metadata">

**Author:** ![Ashu](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Ashu](https://discourse.julialang.org/u/Ashu)\
**Post date:** [October 1, 2023, 6:52pm UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/4 "2023-10-01T18:52:20Z")

</div>

Dear @Ahmed_Salih,  
Have you found a solution to remove those triangular lines? If yes, could you let me know how to do it?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 21, 2023, 4:04pm UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/5 "2023-10-21T16:04:53Z")

</div>

Hi!

Unfortunately, I don’t recall being able too 🙂

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 21, 2023, 5:46pm UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/6 "2023-10-21T17:46:55Z")

</div>

FWIW, one possible workaround in Plots.jl’s gr() backend, is to define `linewidth=0` in the `mesh3d()` code above and to draw the cube edges:

 ![Plots_gr_colored_cube_with_all_edges](https://global.discourse-cdn.com/julialang/original/3X/7/3/73dc6205ecd3a7246b896086a86973c920649bb2.png)

_PS: one red marker was placed in the center of the cube_

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 21, 2023, 6:13pm UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/7 "2023-10-21T18:13:01Z")

</div>

Oh, I would have loved to have known this February 12th 2023, but atleast I know it now 😃

Thanks @rafael.guerra !

---

<div class="post-metadata">

**Author:** ![Ashu](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Ashu](https://discourse.julialang.org/u/Ashu)\
**Post date:** [October 25, 2023, 4:08am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/8 "2023-10-25T04:08:12Z")

</div>

Thank you @rafael.guerra. After using `linewidth=0` in the `mesh3d()` code above, diagonal lines are removed but cube edges are also removed. I want to keep cube edges.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 25, 2023, 5:55am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/9 "2023-10-25T05:55:45Z")

</div>

@Ashu, the workaround suggested requires drawing the edges.

---

<div class="post-metadata">

**Author:** ![Ashu](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Ashu](https://discourse.julialang.org/u/Ashu)\
**Post date:** [January 13, 2024, 4:37am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/10 "2024-01-13T04:37:37Z")

</div>

Dear @rafael.guerra,

Can you please provide the code for this plot? I am getting a cube but with no color fill.

Thank you

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 13, 2024, 8:56am UTC](https://discourse.julialang.org/t/how-to-plot-a-cube-in-3d-in-plots-jl/86919/11 "2024-01-13T08:56:32Z")

</div>

@Ashu, as requested here is the code snippet used.

Note that, as usual with `lw=0`, what you see on the screen is not what you get on disk. The latter is better.

> **Plots.jl gr(): plot a cube**
>
> ```julia
> using Plots; gr()
> 
> xp = [0, 0, 0, 0, 1, 1, 1, 1]
> yp = [0, 1, 0, 1, 0, 0, 1, 1]
> zp = [0, 0, 1, 1, 1, 0, 0, 1]
> connections = [(1,2,3), (4,2,3), (4,7,8), (7,5,6), (2,4,7), (1,6,2), (2,7,6), (7,8,5), (4,8,5), (4,5,3), (1,6,3), (6,3,5)]
> 
> xe = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0]
> ye = [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1]
> ze = [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1]
> 
> plot(xe,ye,ze; lc=:black, lw=0.5, lims=(-0.25,1.25))
> scatter!((0.5,0.5,0.5); c=:red, ms=6, msw=0.1)
> mesh3d!(xp,yp,zp; connections, proj_type=:persp, fc=:lime, lc=:lime, fa=0.3, lw=0)
> 
> savefig("Plots_gr_colored_cube_with_all_edges.png")
> 
> ```
