Plot

Polyhedra contains utilities to visualize either a 2-dimensional or a 3-dimensional polyhedron, see Polyhedron for how to construct a polyhedron, e.g. from its H- or V-representation.

2D plotting with Plots

A 2-dimensional polyhedron can be visualized either

  • with Plots if it is bounded or
  • with MeshCat or Makie whether it is bounded or not (if it is not bounded, it will be truncated).

In this section, we show how to plot 2-dimensional polytopes with Plots. The procedure for plotting 2-dimensional polyhedra with MeshCat or Makie is identical to the plotting of 3-dimensional polyhedra; see the 3D section below.

Suppose for instance that we want to visualize the polyhedron having the following H-representation:

using Polyhedra
h = HalfSpace([1, 1], 1) ∩ HalfSpace([-1, 0], 0) ∩ HalfSpace([0, -1], 0)
H-representation Polyhedra.Intersection{Int64, Vector{Int64}, Int64}:
3-element iterator of HalfSpace{Int64, Vector{Int64}}:
 HalfSpace([1, 1], 1)
 HalfSpace([-1, 0], 0)
 HalfSpace([0, -1], 0)

The H-representation cannot be given to Plots directly, it first need to be transformed into a polyhedron:

p = polyhedron(h)
Polyhedron DefaultPolyhedron{Rational{BigInt}, Polyhedra.Intersection{Rational{BigInt}, Vector{Rational{BigInt}}, Int64}, Polyhedra.Hull{Rational{BigInt}, Vector{Rational{BigInt}}, Int64}}:
3-element iterator of HalfSpace{Rational{BigInt}, Vector{Rational{BigInt}}}:
 HalfSpace(Rational{BigInt}[1, 1], 1//1)
 HalfSpace(Rational{BigInt}[-1, 0], 0//1)
 HalfSpace(Rational{BigInt}[0, -1], 0//1)

The polyhedron can be given to Plots as follows. We use ratio=:equal so that the horizontal and vertical axis have the same scale.

using Plots
plot(p, ratio=:equal)
Example block output

See Polyhedral Function and 3D Plotting a projection of the 4D permutahedron for example notebooks.

3D plotting with Plots

A 3-dimensional polyhedron can be visualized with either MeshCat or Makie. Unbounded polyhedron are supported by truncating the polyhedron into a polytope and not triangularizing the faces in the directions of unbounded rays.

Suppose for instance that we want to visualize the polyhedron having the following H-representation:

julia> using Polyhedra

julia> v = convexhull([0, 0, 0]) + conichull([1, 0, 0], [0, 1, 0], [0, 0, 1])
V-representation Polyhedra.Hull{Int64, Vector{Int64}, Int64}:
1-element iterator of Vector{Int64}:
 [0, 0, 0],
3-element iterator of Ray{Int64, Vector{Int64}}:
 Ray([1, 0, 0])
 Ray([0, 1, 0])
 Ray([0, 0, 1])

The V-representation cannot be given to MeshCat or Makie directly, it first need to be transformed into a polyhedron:

julia> p = polyhedron(v)
Polyhedron DefaultPolyhedron{Rational{BigInt}, Polyhedra.Intersection{Rational{BigInt}, Vector{Rational{BigInt}}, Int64}, Polyhedra.Hull{Rational{BigInt}, Vector{Rational{BigInt}}, Int64}}:
1-element iterator of Vector{Rational{BigInt}}:
 Rational{BigInt}[0, 0, 0],
3-element iterator of Ray{Rational{BigInt}, Vector{Rational{BigInt}}}:
 Ray(Rational{BigInt}[1, 0, 0])
 Ray(Rational{BigInt}[0, 1, 0])
 Ray(Rational{BigInt}[0, 0, 1])

The polyhedron can then be plotted with MeshCat as follows

julia> using MeshCat

julia> m = Polyhedra.Mesh(p)

julia> vis = Visualizer()

julia> setobject!(vis, m)

julia> open(vis)

Note that the Mesh object should be created after loading the plotting package:

Polyhedra.MeshFunction
Mesh(p::Polyhedron)

Returns wrapper of a polyhedron suitable for plotting with MeshCat.jl and Makie.jl.

Extension in Julia 1.9 and above

Although we require using GeometryBasics to use this function in Julia 1.9 and above, in most use cases this extension dependency is loaded by the plotting package and no further action is required.

source

To plot it in a notebook, replace open(vis) with IJuliaCell(vis).

To plot it with Makie instead, you can use for instance mesh or wireframe.

julia> import Makie

julia> Makie.mesh(m, color=:blue)

julia> Makie.wireframe(m)

See 3D Plotting a projection of the 4D permutahedron for an example notebook.