Clear Filters
Clear Filters

how to plot contour for arbitrary shape (not rectangular) in matlab?

5 views (last 30 days)
Hello guys, I have a question for you
In relation to drawing the contour of a non-rectangular shape, what method should be adopted?
For example, how can you draw the contour of the following figure?
If our data is the coordinates of each point (x,y) and the value of Z at each point.
Thank you all.

Answers (2)

Walter Roberson
Walter Roberson on 28 Apr 2024
Use a rectangular array of Z, but set it to NaN outside of the area of interest.
  1 Comment
sajad sajad
sajad sajad on 29 Apr 2024
Hi Mr. Roberson, Oh right, thanks
But my mesh are not regular, so that the matrix can't be used!!

Sign in to comment.


William Rose
William Rose on 28 Apr 2024
You can make a MxN grid of points that cover the X-Y extent of your shape, and define the Z values at those points,. Then you can use contour or surf.
Example:
M=10; N=20;
[X,Y]=meshgrid(0:N,0:M);
Z=exp(-((X-10).^2+(Y-5).^2)/8);
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
Now adjust the grid x and y coordinates (only adjust the Y values in this example):
for j=1:5,Y(:,j)=Y(:,j)/2; end
for j=6:10,Y(:,j)=Y(:,j)*j/10; end
for j=11:15,Y(:,j)=Y(:,j)*(21-j)/10; end
for j=16:21,Y(:,j)=Y(:,j)/2; end
Make another figure with the adjusted grid coordinates:
figure
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
Good luck!
  2 Comments
William Rose
William Rose on 28 Apr 2024
You could also reverse the order in the example above: first adjust the X,Y coordinates of the grid, then deifne the Z values on the adjusted X,Y grid.
M=10; N=20;
[X,Y]=meshgrid(0:N,0:M);
for j=1:5,Y(:,j)=Y(:,j)/2; end
for j=6:10,Y(:,j)=Y(:,j)*j/10; end
for j=11:15,Y(:,j)=Y(:,j)*(21-j)/10; end
for j=16:21,Y(:,j)=Y(:,j)/2; end
Z=exp(-((X-10).^2+(Y-4).^2)/20); % define Z values on adjusted grid
Make figure:
figure
surf(X,Y,Z,EdgeColor='none');
xlabel('X'); ylabel('Y')
axis equal; hold on; view(0,90)
contour3(X,Y,Z,'-k',LineWidth=1)
OK

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!