Plot 2D contour of z at (x,y) coordinates

조회 수: 10 (최근 30일)
Ying Wu
Ying Wu 2022년 12월 29일
댓글: Voss 2022년 12월 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)
scatter figure (what I generate using scatter())
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]);

답변 (2개)

Walter Roberson
Walter Roberson 2022년 12월 29일

Voss
Voss 2022년 12월 30일
load P
load xc
load rc
n_rows = numel(unique(rc));
xcM = reshape(xc,n_rows,[]);
rcM = reshape(rc,n_rows,[]);
PM = reshape(P,n_rows,[]);
contourf(xcM,rcM,PM,'EdgeColor','none')
axis equal tight, caxis(max(abs(caxis))*[-1 1]);
xlabel('xc'); ylabel('rc');
xlim([0 10]); ylim([0 2]);
  댓글 수: 2
Ying Wu
Ying Wu 2022년 12월 30일
@Voss Thanks! But my data is not regular like this (39*175), so I cannot form a coordinate matrix.
Voss
Voss 2022년 12월 30일
@Ying Wu: Hmm, it seems regular:
load P
load xc
load rc
isequal(rc,repmat(unique(rc),1,175))
ans = logical
1
isequal(xc,repelem(unique(xc),1,39))
ans = logical
1
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 CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by