Plots.jl
Plots.jl copied to clipboard
[BUG] mesh3d fails for a simple mesh
using Plots, TriplotRecipes
p = Float64[
-1.0 1.0 1.0 -1.0 0.0 0.0 -1.0 0.0 1.0
-1.0 -1.0 1.0 1.0 -1.0 0.0 0.0 1.0 0.0
]
t = Int32[
1 2 4 5 3 4 2 8
5 6 7 6 8 6 9 6
7 5 6 7 9 8 6 9
]
x, y = p[1,:], p[2,:]
z = x.^2 + y.^2 .- 1
display(plot(x, y, z, seriestype=:mesh3d, faces=t'.- 1, color=:lightblue))
Causes error:
ERROR: ArgumentError: Unsupported `:connections` type Nothing for seriestype=mesh3d
Sometimes the error does not appear, but then the plot is not correct
So a few things
- The argument for
mesh3disconnectionsto define the triangles, notfaces. - Julia uses 1-based indexing so no need to subtract one from your coordinates (which for some reason plots, though not sensible).
- The coordinate indexing needs to be
Int, notInt32(haven't tracked where that limitation is encoded). Just removing explicit typing works fine (or if you have it asInt32from elsewhere,Int.(t)will also work). connectionsneeds to be aAVec{AVec{Int}}rather thanMatrix{Int}so wrapping witheachcolgets it working
With that in mind, the following seems to work cleanly (adding in some alpha so it can be seen more clearly)
using Plots
p = [
-1.0 1.0 1.0 -1.0 0.0 0.0 -1.0 0.0 1.0
-1.0 -1.0 1.0 1.0 -1.0 0.0 0.0 1.0 0.0
]
t = [
1 2 4 5 3 4 2 8
5 6 7 6 8 6 9 6
7 5 6 7 9 8 6 9
]
x, y = p[1,:], p[2,:]
z = x.^2 + y.^2
mesh3d(x,y,z,connections=eachcol(t), fillcolor=:blue, linecolor=:black, fillalpha=0.2)