How to plot the difference between two plots (using shade)

Hello everyone, I have two plots like this:
plot(OBS,'Color','#007aa5')
hold on
plot(Modeled,'Color','#ff55a3')
xlabel('time step')
Now I want to illustrate the difference between these two plots by shade like the following example:
Where plot 2 is less than plot1, the difference determines by yellow sahde and where plot 2 is greater than plot1difference determine using green shade.
I attached my datasets,
Thank you in advance

 Accepted Answer

LD1 = load('OBS[1].mat');
OBS = rmmissing(LD1.OBS);
LD2 = load('Modeled[1].mat');
Modeled = rmmissing(LD2.Modeled);
x = 1:numel(OBS);
figure
plot(OBS,'Color','#007aa5')
hold on
plot(Modeled,'Color','#ff55a3')
patch([x(:); flip(x(:))], [OBS; flip(Modeled)], 'b', 'FaceAlpha',0.25, 'EdgeColor','none')
xlabel('time step')
xlim([min(x) max(x)])
I am not certain what the ‘y’ axis is supposed to be. If the intent is that in increases from the top to the bottom (instead of the default shown here),. provide the appropriate axis tick values, then add:
set(gca, 'YDir','reverse')
.

4 Comments

Thank you so much, that's what I looking for. Just one thing remains, is it possible to illustrate the negative differences with one colour and positive differences with another colour? just like this picture below:
I searched and find your answer to this question and based on that I tried to do it, here is my try:
Lv = OBS > Modeled;
patch([x(:); fliplr(x(:))], [OBS(:) fliplr(Modeled(:))].', 'b', 'FaceAlpha',0.25, 'EdgeColor','none')
patch([x(~Lv); fliplr(x(~Lv))], [OBS(~Lv) fliplr(Modeled(~Lv))].', 'k', 'FaceAlpha',0.25, 'EdgeColor','none')
But the error says:
Error using patch
Vectors must be the same length.
Is it possible to do that?
Thank you again for your valuable help.
As always, my pleasure!
LD1 = load('OBS[1].mat');
OBS = rmmissing(LD1.OBS);
LD2 = load('Modeled[1].mat');
Modeled = rmmissing(LD2.Modeled);
x = 1:numel(OBS);
x = x(:); % Force Column Vector
Lv = OBS > Modeled; % Logical Vector
ixs = strfind(Lv', [0 1])+1; % Start
ixe = strfind(Lv', [1 0]); % Stop
ix1m = [ixs(:) ixe(:)]; % Blue Sections
ixs = strfind([false;~Lv]', [0 1]); % Start
ixe = strfind([false;~Lv]', [1 0]); % Stop
ix2m = [ixs(1:numel(ixe)); ixe]'; % Yellow Sections
figure
plot(OBS,'Color','#007aa5')
hold on
plot(Modeled,'Color','#ff55a3')
for k = 1:size(ix1m,1)
idxrng = ix1m(k,1):ix1m(k,2);
patch([x(idxrng); flip(x(idxrng))], [OBS(idxrng); flip(Modeled(idxrng))], 'b', 'FaceAlpha',0.5, 'EdgeColor','none')
end
for k = 1:size(ix2m,1)
idxrng = ix2m(k,1):ix2m(k,2);
patch([x(idxrng); flip(x(idxrng))], [OBS(idxrng); flip(Modeled(idxrng))], 'y', 'FaceAlpha',0.5, 'EdgeColor','none')
end
hold off
xlim([min(x) max(x)])
This code is fragile because of the way the indices need to be calculated (the ‘start’ and ‘stop’ index vectors must have equal lengths for each colour), so I can’t guarantee that it will work for all such vectors without modification. The ‘ix1m’ vector worked straight away, however the ‘ix2m’ vector needed modification.
The resolution could be improved (if desired) by interpolating, for example:
xq = linspace(min(x), max(x), numel(x)*10);
OBSnew = interp1(x, OBS, xq)
OBSnew = 1×3550
-0.6714 -0.6940 -0.7166 -0.7392 -0.7618 -0.7844 -0.8070 -0.8296 -0.8522 -0.8748 -0.8974 -0.9109 -0.9241 -0.9373 -0.9505 -0.9637 -0.9769 -0.9902 -1.0034 -1.0166 -1.0298 -1.0179 -1.0047 -0.9915 -0.9783 -0.9651 -0.9519 -0.9386 -0.9254 -0.9122
That would eliminate some of the gaps that are visible on zooming, and are due to the current resolution. Do the same with ‘Modeled’ and use ‘xq’ instead of ‘x’ in the plot and patch calls.
.
I really appreciate you.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

BN
on 3 Oct 2021

Commented:

on 5 Oct 2021

Community Treasure Hunt

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

Start Hunting!