Filled 2D plot
조회 수: 4 (최근 30일)
이전 댓글 표시
I have 3 vectors, X,Y,Z in which X,Y generates one plot and X,Z generates another. However, what I want to do is plot X,Y as a 2D plot and fill the area between the curve and the X axis with colors based on the Z values. I tried using the contour plot but failed. However, then I managed to change the colour of the curve based on the Z value as follows, but I need the complete area under the curve to be filled. Is there any advice?
c= cos(Z).^2;
colormap(hsv)
patch(X,Y,Z,c,'FaceColor','none','EdgeColor','interp')
댓글 수: 2
DGM
2021년 4월 16일
Please provide a sufficient amount of code to constitute an example of what you're trying to do.
채택된 답변
Star Strider
2021년 4월 17일
My apologies for the delay. I was working on other things most of today.
‘However, to make the data more concise, I am trying to plot a single figure of X vs Y in which, the area under the curve is colored based on the value of Z at that region.’
This was a bit of a challenge! However it seems to do what was requested.
The Code —
XL = load('X.mat');
YL = load('Y.mat');
ZL = load('Z.mat');
X = XL.X;
Y = YL.Y;
Z = ZL.Z;
figure
plot(X, Y)
hold on
patch([X; flipud(X)], [Y; zeros(size(Y))], [Z; flipud(Z)])
hold off
grid
cb = colorbar;
cb.Label.String = 'Z Value';
xlabel('X')
ylabel('Y')
title('Full Patch Plot')
figure
plot(X, Y)
hold on
patch([X; flipud(X)], [Y; zeros(size(Y))], [Z; flipud(Z)])
hold off
grid
cb = colorbar;
cb.Label.String = 'Z Value';
xlabel('X')
ylabel('Y')
title('Zoom Detail Patch Plot')
axis([0 20 0 5E-3])
figure
plot(X, Z)
grid
The Plots —


The ‘Z’ value is very high only in a very small part of the patch object, so the ‘zoomed’ plot is necessary to demonstrate that the code does what was requested. The maximum value of ‘Z’ occurs at an ‘X’ value of about 3.22, and that is where the colour changes the most. The ‘Z’ value is actually quite noisy, accounting for the ‘striated’ appearance of the patch colours.
댓글 수: 8
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!