how to plot contour for arbitrary shape (not rectangular) in matlab?
16 次查看(过去 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.
0 个评论
回答(2 个)
Walter Roberson
2024-4-28
Use a rectangular array of Z, but set it to NaN outside of the area of interest.
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!