# Draw a surface using Gaston with a nonrectangular domain

**URL:** <https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825>\
**Category:** New to Julia\
**Tags:** plotting, gaston\
**Created:** [April 8, 2021, 12:38pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825 "2021-04-08T12:38:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 12:38pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/1 "2021-04-08T12:38:51Z")

</div>

Dear all,  
I have the code:

```julia
using Gaston

X = LinRange(-1,1, 30) # 50
Y = LinRange(-1,1, 30)

x = [u for u in X, v in Y]
y = [v for u in X, v in Y]
z = [u^2+v^2 for u in X, v in Y]

#
surf(x, y, z, w = :l, lc = :turquoise,
       Axes(xlabel = :x, ylabel = :y, zlabel = :z,zrange= "[*<0:1<*]",xrange= "[*<-1:1<*]",yrange= "[*<-1:1<*]",
           #view= "60, 45, 1, 1",
            xtics = """("-0.5" -0.5, "0.0" 0, "+0.5" 0.5) offset .25,-0.5""",
            ytics = """("-0.5" -0.5, "0.0" 0, "+0.5" 0.5) offset -1.25,-0.5""",
            view = "75, 30, 0.7, 2.1",
            size = "1.1,1.",
            xyplane = "at 0",
            #pm3d = "depthorder",
            hidden3d = :on,
            key = :off))

```

Supose that I want to draw this surface in a nonrectangular domain, eg, for all u \in [-1,1] and 0 \leq v \leq 1-u. How can I adapt the code above?

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 8, 2021, 6:49pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/2 "2021-04-08T18:49:51Z")

</div>

I am not sure I understand the second inequality, since u is a range, not a constant. However, I think this example may help you:

Gnuplot does not need a rectangular domain to plot; all it needs is a list of coordinates. Let’s say we want to plot a circle; we can do it as follows:

```julia
Θ = LinRange(0, 2π, 100);
x1 = [cos(θ) for θ in Θ];
y1 = [sin(θ) for θ in Θ];
z1 = [1 for θ in Θ];
surf(x1, y1, z1)

```

This plot results:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/895f8275c7614227ec436f9b10ca5751952548a4.png)  
Let’s add an oval:

```julia
x2 = [2cos(θ)+1 for θ in Θ];
y2 = [sin(θ)+1 for θ in Θ];
z2 = [2 for θ in Θ];
surf!(x2, y2, z2)

```

with the result:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/5/c56db7ba0ad873f18c7e98674dfd3e83e9d45040.png)

Both plots have clearly non-rectangular domains. It’s often more convenient to think about sets of arbitrary `(x,y,z)` coordinates rather than a 2-D function evaluated on a matrix.

If you want a surface, then you can put the sets of coordinates in the rows of a matrix. Gnuplot will then draw lines between the corresponding sets of points:

```julia
surf([x1';x2'], [y1';y2'], [z1';z2'])

```

results in:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/4/340d241302523bfbe52d444a8a2349e308517419.png)  
which is, again, a plot on a non-rectangular domain.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 7:06pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/3 "2021-04-08T19:06:31Z")

</div>

> [@mbaz](#):
>
> `surf([x1';x2'], [y1';y2'], [z1';z2'])`

Thank you so much for your response.  
However, you don’t underestood my question.

Lets to think in a domain in \mathbb{R}^2 where 0 \leq x \leq 1 and 0 \leq y \leq x. Do you agree with me that we don’t have an rectangle? The region is a triangle, whose vertex are the points (0,0), (1,0) and (1,1).

My intention is plot the graph of a given function z=f(x,y) where (x,y) are in this triangle (the extremes of this region are not constants). Given a x in the rangle [0,1], the y-value varies in the range [0,x].

We usually plot a surface when the extremes are constants. But, in this case, how can we proceed?

What I need to adapt in my code to plot this suface?  
Thank you.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 8, 2021, 7:28pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/4 "2021-04-08T19:28:43Z")

</div>

Got it. I think the easiest way is to use the fact that gnuplot ignores NaNs when plotting.

```julia
f(x,y) = y < x ? cos(8x) + sin(8y) : NaN
x = LinRange(0, 1, 100);
y = LinRange(0, 1, 100);
surf(x, y, f, w = :pm3d)

```

produces this plot:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed9da7ffe5e46080d107ff79faef3093283b5cd4.png)  
with a triangular domain.

The downside of this approach is that you have to redefine (or wrap) the function to return `NaN` for values outside the range you’re interested in. Pinging @lazarusA; he might know a better way.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 8:15pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/7 "2021-04-08T20:15:10Z")

</div>

The initial code is the following:

```julia
using Gaston

f(x,y) = y < x && y > x^2 ? x^2+y^2 : NaN
x = LinRange(-1, 1, 100);
y = LinRange(-1, 1, 100);

surf!(x, y, f,w = :pm3d)

```

But I want to plot without use the structure `if-else`. Is there another way to do this? Thank you!

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 8, 2021, 8:27pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/8 "2021-04-08T20:27:32Z")

</div>

I can’t think of any. Any particular reason why you want to avoid it?

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 8:33pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/9 "2021-04-08T20:33:17Z")

</div>

If you have a little points, the is a problem; see below:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/6/e65b10d1fd4f80fedb6d003d86e33e584ec8ef11.png)

a little strange…

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [April 8, 2021, 9:15pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/10 "2021-04-08T21:15:24Z")

</div>

> [@angeloaliano1](#):
>
> ```julia
> f(x,y) = y < x && y > x^2 ? x^2+y^2 : NaN
> x = LinRange(-1, 1, 100);
> y = LinRange(-1, 1, 100);
> 
> ```

> The region is a triangle,

Then I would do a Mesh, unfortunately today is already late. Maybe tomorrow I can take a look at it again.

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 9:17pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/11 "2021-04-08T21:17:30Z")

</div>

Thank you so much!  
Can be tomorrow.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 8, 2021, 9:57pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/12 "2021-04-08T21:57:52Z")

</div>

> [@angeloaliano1](#):
>
> If you have a little points, the is a problem

Is the problem that the calculation is inefficient because most points are ignored? Or is it something else?

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [April 8, 2021, 10:00pm UTC](https://discourse.julialang.org/t/draw-a-surface-using-gaston-with-a-nonrectangular-domain/58825/13 "2021-04-08T22:00:10Z")

</div>

A more number of points, the calculation is ineficient.  
To produce the figure below, I have used 1000 points on x and 1000 points in y.  
I have a solution. But, it isn’t the better way…

![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a659c4f4e33a1dddae029464d05ccec22d65c14.png)
