# Mesh3D Only Plotting Half

**URL:** <https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760>\
**Category:** Visualization\
**Created:** [January 3, 2021, 6:13am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760 "2021-01-03T06:13:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 6:13am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/1 "2021-01-03T06:13:39Z")

</div>

Hello, I’m trying to plot a 3d surface. I’ve used a marching cubes algorithm to generate a tuple of points. When I plot them in scatter() I get the following image, which is what I want and expect.

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

```julia
scatter([a for (a,_,_) in points],[b for (_,b,_) in points], [c for (_,_,c) in points] )

```

However when I try and use Mesh3d to generate the mesh surface I obtain the following.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/7/777bdae8292c204b60b8d55c752971b6e39d135a.png)

```julia
mesh3d([a for (a,_,_) in points], [b for (_,b,_) in points], [c for (_,_,c) in points])

```

And only half of the points appear to be plotted. Is there a reason for this? I’m using Plots with a PlotlyJS backend. The entire setup is running in a pluto notebook, and I can append the code if someone wants to see the whole thing. The MWE is immediately below, along with a .csv version of the points tuple variable beneath it that one should be able to copy and paste!

```julia
using Plots
	Pkg.add("PlotlyJS")
Pkg.add("QuadGK")
	using QuadGK
	plotly()
Pkg.add("Meshing")
	using Meshing

function unknotintegralfunction(a,b,c,t)
	1/(sqrt(((a.-cos.(t))^2).+((b.-sin.(t))^2).+c^2)) #using .math means element wise
end

function unknotpotential(a,b,c,A) 
	ans, err = quadgk(t->unknotintegralfunction(a,b,c,t),0,(2*pi),order=quadorder) 
	# we'll use the default errors to start
	A[floor(Int,(10*a+21)),floor(Int,(10*b+21)),floor(Int,(10*c+21))]=ans
	return [a,b,c,ans,err]
end

inflateunknot(f,N,p,A)=[f(x,y,z,A) for x in -1*N:p:N, y in -1*N:p:N, z in -1*N:p:N if z ≠ 0 || (x*x +y*y)≠ 1]
A = zeros(41,41,41)
ans = inflateunknot(unknotpotential,2,0.1,A)
c=A[20,20,20]+0.5
points,surfaces = isosurface(A,MarchingCubes(iso=c), origin = Vec(1,1,1), widths = Vec(4.0,4.0,4.0)) 

scatter([a for (a,_,_) in points],[b for (_,b,_) in points], [c for (_,_,c) in points] )
mesh3d([a for (a,_,_) in points], [b for (_,b,_) in points], [c for (_,_,c) in points])

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 3, 2021, 6:36am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/2 "2021-01-03T06:36:45Z")

</div>

Please provide a MWE (minimal working example that people can copy and paste to reproduce the problem, including all data and `using` calls). See [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). In particular you haven’t said which of the many plotting libraries you are using.

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 6:42am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/3 "2021-01-03T06:42:24Z")

</div>

My apologies, I’ve edited the original question to include the code generating the data to be plotted and the plots themselves along with details about the plotting libraries.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 3, 2021, 6:51am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/4 "2021-01-03T06:51:02Z")

</div>

Thanks! You might want to reduce that down to normal Julia code, rather than a Pluto notebook, and reduce it down to the bare minimum that reproduces the problem

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 6:58am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/5 "2021-01-03T06:58:40Z")

</div>

Ah, will do! Was worried there might be something in the pluto notebook causing the issue itself. I’ll update tomorrow with a stripped down example.

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 6:53pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/6 "2021-01-03T18:53:50Z")

</div>

I’ve edited the question to include an MWE, I followed the guide you linked to to output a copy-pastable version of my points variable. However it’s too many points to just copy paste and include in the example, not too sure how I should go about providing the variable values.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 3, 2021, 7:23pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/7 "2021-01-03T19:23:04Z")

</div>

I don’t see the `points` variable. It’s fine / better to generate that using code, not copy paste the data.

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 8:14pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/8 "2021-01-03T20:14:30Z")

</div>

Hello again! I appreciate your patience, I’ve updated the code with what I think is enough to generate the dataset. It works on my machine so hopefully it reproduces on yours!

---

<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 3, 2021, 9:46pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/9 "2021-01-03T21:46:13Z")

</div>

The code provided would not run in my machine “as is”. The edited code below runs. Need to set alphahull in mesh3d.

```julia
using Plots, PlotlyJS, QuadGK, Meshing, StaticArrays
plotly()

function unknotintegralfunction(a,b,c,t)
	1/(sqrt( (a - cos(t))^2 + (b - sin(t))^2 + c^2 ))
end

function unknotpotential(a,b,c,A) 
	ans0, err = quadgk(t->unknotintegralfunction(a,b,c,t),0,2π,order=quadorder) 
	A[floor(Int,(10*a+21)),floor(Int,(10*b+21)),floor(Int,(10*c+21))] = ans0
	return [a,b,c,ans0,err]
end

inflateunknot(f,N,p,A) = [f(x,y,z,A) for x in -N:p:N, y in -N:p:N, z in -N:p:N if z ≠ 0 || (x*x +y*y)≠ 1]

quadorder = 20
A = zeros(41,41,41)
ans = inflateunknot(unknotpotential,2,0.1,A)
c = A[20,20,20] + 0.5
points,surfaces = isosurface(A,MarchingCubes(iso=c), origin = SVector(1,1,1), widths = SVector(4.0,4.0,4.0))

scatter([a for (a,_,_) in points],[b for (_,b,_) in points], [c for (_,_,c) in points] )

PlotlyJS.plot(
    PlotlyJS.mesh3d(
        x = [a[1] for a in points],
        y = [a[2] for a in points],
        z = [a[3] for a in points],
        alphahull = 5
    )
)

```

 ![mesh3d_alphahull5](https://global.discourse-cdn.com/julialang/original/3X/8/2/82495350f57a52d727c1ba4718f4e8450968fe3a.png)

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 3, 2021, 9:54pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/10 "2021-01-03T21:54:17Z")

</div>

Thank you Rafael, I appreciate the time and effort. I’ll take a look at your code and see where I made some mistakes in my own. With regards to the mesh, is it possible that I’m feeding too many points to Mesh3d? Would the algorithm have an easier time with a sparser dataset?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 3, 2021, 11:01pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/11 "2021-01-03T23:01:14Z")

</div>

It looks to me like the points are not being generated in the right order for the meshing algorithm to parse.

What happens if you replace `plotly` by `plotlyjs`? IIUC, the former is remote and the latter is local.

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 4, 2021, 12:56am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/12 "2021-01-04T00:56:20Z")

</div>

switching to PlotlyJS creates a number of errors that take me to the following [webpage](https://juliagizmos.github.io/WebIO.jl/latest/troubleshooting/not-detected/).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/a/7a9d9cd91a4f788e90be932b7f9ce244b6ec3480.png)  
It discusses Jupyter notebooks but makes no mention of Pluto. Installing the dependencies as below unfortunately does not resolve the error.

```julia
begin
	Pkg.add("WebIO")
	using WebIO
end

```

Is the PlotlyJS backend not supported for Pluto? If so I might stick with a 3d scatter plot for now rather than worrying about trying to get mesh3d working in a notebook.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 4, 2021, 5:37am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/13 "2021-01-04T05:37:45Z")

</div>

You could try with WGLMakie.

---

<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 4, 2021, 7:01am UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/14 "2021-01-04T07:01:39Z")

</div>

The default `alphahull` parameter was not appropriate. Please check my edited post above.

---

<div class="post-metadata">

**Author:** ![Nkrause](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@Nkrause](https://discourse.julialang.org/u/Nkrause)\
**Post date:** [January 4, 2021, 4:40pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/15 "2021-01-04T16:40:12Z")

</div>

Thank you Rafael!

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [January 6, 2021, 3:35pm UTC](https://discourse.julialang.org/t/mesh3d-only-plotting-half/52760/16 "2021-01-06T15:35:44Z")

</div>

@Nkrause WebIO is not enabled in Pluto: [https://github.com/fonsp/Pluto.jl/issues/299](https://github.com/fonsp/Pluto.jl/issues/299)
