Plots.jl: How to plot a cube in Julia using Plots?

Created on 27 Apr 2018  路  2Comments  路  Source: JuliaPlots/Plots.jl

I am sorry, if this is totally misplaced but I did not find any clue in the docs or the examples.
For example I want to plot a cube with vertices (0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0),(1,1,0),(1,0,1),(1,1,1) such that I get a cube with coloured facets, not just the vertices or lines.
How can I do this?

Just something like this, the facets may have a all the same colour.
grafik

Thanks for your help, I really like the Plots package!

Most helpful comment

I've found the easiest way to do this at the moment is with PlotlyJS.
Here's the julia code to plot a red unit cube:

using PlotlyJS

function mesh_box(l,w,h,x,y,z)
    PlotlyJS.mesh3d(
        x = [x, x+l, x,   x+l, x,   x+l, x,   x+l],
        y = [y, y,   y+w, y+w, y,   y,   y+w, y+w],
        z = [z, z,   z,   z,   z+h, z+h, z+h, z+h],
        i = [0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7],
        j = [3, 3, 5, 5, 6, 6, 3, 3, 5, 5, 6, 6],
        k = [1, 2, 1, 4, 2, 4, 1, 2, 1, 4, 2, 4],
        color = "rgb(200,100,100)",
    )
end

PlotlyJS.plot(
    mesh_box(1,1,1,0,0,0)
)

More examples (in python) available here: https://plot.ly/python/3d-mesh/

You could also investigate ThreeJS, Makie, and MeshCat, but I've found those difficult to install in Julia 1.1

All 2 comments

I've found the easiest way to do this at the moment is with PlotlyJS.
Here's the julia code to plot a red unit cube:

using PlotlyJS

function mesh_box(l,w,h,x,y,z)
    PlotlyJS.mesh3d(
        x = [x, x+l, x,   x+l, x,   x+l, x,   x+l],
        y = [y, y,   y+w, y+w, y,   y,   y+w, y+w],
        z = [z, z,   z,   z,   z+h, z+h, z+h, z+h],
        i = [0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7],
        j = [3, 3, 5, 5, 6, 6, 3, 3, 5, 5, 6, 6],
        k = [1, 2, 1, 4, 2, 4, 1, 2, 1, 4, 2, 4],
        color = "rgb(200,100,100)",
    )
end

PlotlyJS.plot(
    mesh_box(1,1,1,0,0,0)
)

More examples (in python) available here: https://plot.ly/python/3d-mesh/

You could also investigate ThreeJS, Makie, and MeshCat, but I've found those difficult to install in Julia 1.1

Thank you very much, that should work as wished =)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbenst picture tbenst  路  3Comments

Cody-G picture Cody-G  路  3Comments

crstnbr picture crstnbr  路  3Comments

SebastianM-C picture SebastianM-C  路  4Comments

Krastanov picture Krastanov  路  3Comments