Plot 2D contour of z at (x,y) coordinates
14 次查看(过去 30 天)
显示 更早的评论
Hi, I have three arrays with same dimension: xc, rc, P, where xc and yc are coordinates, P is pressure value at (xc,rc). Is there anyway to plot P as a 2D contour?
I know some functions like pcolor(), contourf() can do this thing, but they require P to be a matrix, not an array.
I also know scatter() can do similar thing, but I need a contour (see below), not scattered points. I attach my dataset and the color figure with many scattered points. Thanks for any suggestion!
contour figure (what I want)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1247112/image.png)
scatter figure (what I generate using scatter())
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1247122/image.png)
load xc.mat
load rc.mat
load P.mat
scatter(xc,rc,10,P);
axis equal tight, caxis(max(abs(caxis))*[-1 1]);
xlabel('xc'); ylabel('rc');
xlim([0 10]); ylim([0 2]);
0 个评论
回答(2 个)
Voss
2022-12-30
2 个评论
Voss
2022-12-30
@Ying Wu: Hmm, it seems regular:
load P
load xc
load rc
isequal(rc,repmat(unique(rc),1,175))
isequal(xc,repelem(unique(xc),1,39))
But regardless, you can use a scatteredInterpolant (which should give the same result in this case, and should work properly if you do have irregular data):
xcd = double(xc);
rcd = double(rc);
I = scatteredInterpolant(xcd(:),rcd(:),P(:));
[xcM,rcM] = ndgrid(unique(xcd),unique(rcd));
contourf(xcM,rcM,I(xcM,rcM),'EdgeColor','none')
axis equal tight, caxis(max(abs(caxis))*[-1 1]);
xlabel('xc'); ylabel('rc');
xlim([0 10]); ylim([0 2]);
另请参阅
类别
在 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!