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

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:

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:

# 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.

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

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)
2 Likes