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 Comments

DGM
DGM on 16 Apr 2021
Please provide a sufficient amount of code to constitute an example of what you're trying to do.
Ironhide Jr
Ironhide Jr on 16 Apr 2021
Edited: Ironhide Jr on 16 Apr 2021
A brief example of the data would be as,
X=1:22000;
Y=2:2:44000;
Z=rand(1,22000);
Now I can plot X and Y in 2D as well as X and Z in 2D. 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.
I have also attached an actual dataset marked directly as X,Y and Z.
Thank you

Sign in to comment.

 Accepted Answer

Star Strider
Star Strider on 17 Apr 2021
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 Comments

Ironhide Jr
Ironhide Jr on 17 Apr 2021
Hi, thanks for the solution.
Yes this does what I needed. You are right, the Z data is actually noisy but that is part of what I intended to see through the figure.
I was trying the patch as well, but your solution is much more elegant. Really appreciate the support.
Star Strider
Star Strider on 17 Apr 2021
As always, my pleasure!
This was an interesting problem! I have a reasonable amount of experience with patch, however this was entirely new to me.
Ironhide Jr
Ironhide Jr on 17 Apr 2021
Hi, I tried changing the scale of the x axis to logarithmic, but this seems to conflict with the patch. Is there a workaround for this?
Thanks
Yes!
The patch function does not work with semilogx, so plot it with a linear scale first, then use:
set(gca, 'XScale','log')
The solution is also to add 1 to the x-vector, since log(0) is NaN and NaN values do not plot and can prevent patch from working properly.
figure
% plot(X, Y)
hold on
patch([X; flipud(X)]+1, [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')
set(gca, 'XScale','log')
.
Ironhide Jr
Ironhide Jr on 17 Apr 2021
Hi, sorry for the late reply.
Thanks for the explanations in addition to the support.
Thank you
No wories!
As always, my pleasure!
Also, to plot logarithmic scales on both axes, this works —
figure
% plot(X, Y)
hold on
patch([X; flipud(X)]+1, [Y; zeros(size(Y))]+0.1, [Z; flipud(Z)])
hold off
grid
cb = colorbar;
cb.Label.String = 'Z Value';
xlabel('X')
ylabel('Y')
title('Full Patch Plot')
set(gca, 'XScale','log')
set(gca, 'YScale','log')
.
Ironhide Jr
Ironhide Jr on 18 Apr 2021
Thanks man. Really appreciate the support.
Star Strider
Star Strider on 18 Apr 2021
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!