# Smoothing Discontinuities in 3D Plots

**URL:** <https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [March 3, 2021, 7:45pm UTC](https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437 "2021-03-03T19:45:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tross](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tross](https://discourse.julialang.org/u/tross)\
**Post date:** [March 3, 2021, 7:45pm UTC](https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437/1 "2021-03-03T19:45:19Z")

</div>

Hi there, I have a fairly simple problem. I am trying to do a 3D plot of a function that has discontinuities, but I can’t figure out how to get the boundary of the discontinuity to look smooth instead of spikey.

Here is an example code, (I am sure there must be a more elegant way to write this).

```julia
using Plots
plotlyjs()

function f(x,y)
    a = sin.(x).*y
    if a.<=1
        asin.(a)
    else
        NaN
    end
end

f2(x,y)=(x,y,f(x,y))

x=0:.01:pi/2
y=0:.01:2

xm, ym, zm = [[pt[i] for pt in f2.(x,y')] for i in 1:3]

surface(xm,ym,zm)

```

I get a jagged edge at the discontinuity.  
 ![juliasurface](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e68d7b5c69b434727e2d54b0470be9ae1869759.png)

In comparison, when I make the following plot in Mathematica I get a smooth edge.

```julia
Plot3D[ArcSin[Sin[a]*b], {a, 0, Pi/2}, {b, 0, 2}]

```

![mathematicasurface](https://global.discourse-cdn.com/julialang/original/3X/7/7/7747e03e526104db891b709de22aa727857b8e4a.png)

I have tried increasing the sampling for the values of x and y but that still results in a spikey edge. I am guessing there is an interpolation step that the mathematica plotter is doing that I am missing with my script in Julia, but I am not sure how to implement it.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [March 3, 2021, 10:19pm UTC](https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437/2 "2021-03-03T22:19:11Z")

</div>

I would say that Mathematica is very clever, doing what you want instead of what you ask for! 😉 . When I look at the individual points in a scatter plot…

```plaintext
scatter3d(vec(xm),vec(ym),vec(zm),ms=0.5)

```

![orig](https://global.discourse-cdn.com/julialang/original/3X/8/8/88f453355e45e2a88bc376dd386c2117568ebd99.png)

it appears that `Plots` is plotting what is actually there. The best I can think of is to “pin” your function output values rather than set them to NaNs. So, by replacing `NaN` with `π/2` in your function `f`, I get

![pinned](https://global.discourse-cdn.com/julialang/original/3X/8/5/857eab471dbb7c3d6a22738bc495cb645f0bf9bd.png)

---

<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:** [March 4, 2021, 6:36am UTC](https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437/3 "2021-03-04T06:36:47Z")

</div>

Another way is to write y function of x and z.  
_PS: edited the order of the variables to preserve the Cartesian axes right-handedness._

```julia
using Plots; plotlyjs()
g(z, x) = sin(z) ./ sin(x)
x = 1e-3:.01:π/2
z = 0:.01:π/2
Plots.surface(z, x, g, xlabel="z",ylabel="x",zlabel="y"; zlims=(0,2),
    color=:reds, colorbar=false)

```

 ![asin_surface_rg1](https://global.discourse-cdn.com/julialang/original/3X/d/e/de0e5d9fcdc3a093dc39a1220f33f814f0df7782.png)

---

<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:** [March 4, 2021, 9:01am UTC](https://discourse.julialang.org/t/smoothing-discontinuities-in-3d-plots/56437/4 "2021-03-04T09:01:40Z")

</div>

Another way is to use a parametric surface.

```julia
S(x,z) = (x, sin(z)/sin(x), z)
x = 1e-3:.02:π/2
z = 0:.02:π/2
xs, ys, zs = [[p[i] for p in S.(x, z')] for i in 1:3];
using Plots; plotlyjs()
Plots.surface(xs,ys,zs, xlabel="x",ylabel="y",zlabel="z"; ylims=(0,2),colorbar=false)

```

 ![arcsin_parametric_surface_plotlyjs](https://global.discourse-cdn.com/julialang/original/3X/e/c/ec274a9df0a96b8ae0051f95152b665fec35a301.jpeg)  
.  
.  
In Makie this works but the y-axis cannot be properly tamed with `ylims`:

```julia
using Makie
fig = Makie.surface(xs,ys,zs, backgroundcolor=:black)
Makie.ylims!(fig.axis.scene, 0, 2.0) # ylims not really working as expected

```

 ![Makie_asin_parametric](https://global.discourse-cdn.com/julialang/original/3X/8/4/848c581bb390d61091a1b90d16f34e7490032500.jpeg)
