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

7 ビュー (過去 30 日間)
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.

サインインしてコメントする。

### 回答 (2 件)

Walter Roberson 2024 年 4 月 28 日
Use a rectangular array of Z, but set it to NaN outside of the area of interest.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Hi Mr. Roberson, Oh right, thanks
But my mesh are not regular, so that the matrix can't be used!!

サインインしてコメントする。

William Rose 2024 年 4 月 28 日
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 件のコメントなしを表示なしを非表示
William Rose 2024 年 4 月 28 日
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
Hi Mr. Rose, Thanks a lot.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by