plot 2D-function: x and y Range

2 views (last 30 days)
SA-W
SA-W on 10 Nov 2020
Answered: Benjamin Kraus on 11 Jun 2025
Hello,
I have a function of 2 variables (X,Y). Of course, I can plot the function with surf(...).
But the function is only valid for X and Y Values, which describe a triangle. Outside the triangle, I do not want to see the Z-Values in my surface plot (or set the Z-Values to Zero there).
How can I solve that easily?
Thank you!

Answers (2)

Abhishek
Abhishek on 11 Jun 2025
Edited: Abhishek on 11 Jun 2025
Hi @SA-W,
Since you are looking to plot the two variables using the surf command, I suggest you mask the values first, then plot using surf. This is a common approach when working with constrained domains like a triangular region, where you only want to visualize values that fall inside a specific area.
To help illustrate the idea, here's a minimal example I tried on my end. The goal is to plot a function only within a triangle defined by the condition X + Y ≤ 1 (basically a right-angled triangle in the first quadrant).
  • I started off by creating a simple grid that covers the full area, including and around the triangle, using meshgrid:
[x, y] = meshgrid(linspace(0, 1, 100), linspace(0, 1, 100));
  • Then I defined the function and created the mask. After that, I applied the mask.
% Function Definition
z = sin(pi*x) .* cos(pi*y);
% Create a mask for the triangular region
mask = (x + y) <= 1;
  • Apply the mask by setting values outside the triangle to 'NaN':
% Apply the mask: set values outside the triangle to NaN
z(~mask) = NaN;
The key idea here is that ‘NaN’ values are automatically ignored in the plot, so anything outside the region of interest simply will not appear in the plot.
I ran this workaround on MATLAB R2024b, and below is the result:
You can find additional information about meshgridand surf in the MATLAB’s official documentation:
I Hope this helps you.
  2 Comments
DGM
DGM on 11 Jun 2025
Might want to include the code where you show how to use the NaNs when plotting the data.
Abhishek
Abhishek on 11 Jun 2025
Hi @DGM. Thanks for the suggestion. I have added that part as well.

Sign in to comment.


Benjamin Kraus
Benjamin Kraus on 11 Jun 2025
While @Abhishek's solution is a fine solution, there is no restriction that the x and y input to the surf command are the output from the meshgrid command or that they define a rectangle, so an alternative would be to define x and y to only include points within the triangle in the first place.
This is a bit tricker than the solution that @Abhishek provided, because there isn't a single function that defines coordinates of a triangle, and the input to surf does still need to be a regular matrix grid, but it is another option if you want to take it.
For example, this code defines x and y coordinates that represent an equilateral triange centered at 0 with corners that are 1 data point away from the origin. I'm using polar coordinates to define the regions, but you can use any approach to accomplish the same goal.
t = linspace(0,2*pi,4);
cornersx = cos(t');
x = [zeros(size(cornersx)) cornersx]';
cornersy = sin(t);
y = [zeros(size(cornersy)); cornersy];
[XX,YY] = meshgrid(linspace(0,3,31),linspace(0,1,11));
TX = interp2(0:3,[0 1],x,XX,YY);
TY = interp2(0:3,[0 1],y,XX,YY);
ZZ = sin(pi.*TX) .* cos(pi.*TY);
surf(TX,TY,ZZ)
view(340,50)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!