how to make figure that shown below

2 views (last 30 days)
Hossam Mosbah
Hossam Mosbah on 11 May 2022
Commented: KSSV on 13 May 2022
Hi everyone, I want to draw this figure and the olors keep overlapping, so is there any help so I can draw the figure without overlapping. Thanks in advance. The code is below and the data is attached.
SORRY THE CODE WAS WRONG AND NOW IT IS CORRECTED
Here is the code
ttt=aaaa(:,1) % Set the time in minutes for figures % Rescale the time to look good in figures
tss=ttt/60; % Convert time to hours
%% tg is the important x-axis represents hours
tg = hours(tss) + minutes(0); % x Convert time to hours
tg.Format = 'hh:mm'; % Convert time to hours format
% These are three variables represent y-axis
PgridN11=1e6*aaaa(:,2); % y2 the original load forecast without shaving
Pshave=1e6*aaaa(:,3); % y3 the threshold parameter
for i=1:1:288
if PgridN11(i) >= Pshave(i)
Pgrid(i)=0.99*PgridN11(i);
else
Pgrid(i)=PgridN11(i);
end
if Pgrid(i) < Pshave(i)
Pgrid(i)=Pshave(i);
end
end
p1 = PgridN11;
p2 = Pshave;
x = linspace(0, numel(p1)-1, numel(p1));
idx = find(diff(sign(p1-p2))); % Approximate Indices Of The Intersections
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(numel(p1),idx(k)+1);
xi(k) = interp1(p1(idxrng)-p2(idxrng), x(idxrng), 0);
yi(k) = interp1(x(idxrng),p2(idxrng),xi(k));
end
indexOfInterest=round(xi(1):xi(end));
set(gcf,'color','w');
% Plot the threshold with original load and load after shaving
figure(1)
plot(tg,Pgrid,'b','linewidth',1.9); hold on; % tg 'x' y1 'PLShave'
plot(tg,PgridN11,'-.k','linewidth',1.7); hold on; % y2 'PgridN11'
plot(tg,Pshave,'-.r','linewidth',1.5); % y3 'Pshave'
% the peak period
xlim([tg(1) tg(end)])
xlabel('time (hh:mm)');
ylabel('LoadForecating LF (W)');
grid on;
xline(tg(indexOfInterest(1)),'-.b','LineWidth',1.5)
xline(tg(indexOfInterest(end)),'-.b','LineWidth',1.5)
title('LoadForecating LF (W)');
legend('LF after Shaving','Orinignal LF','Threshold level');
%%
% this part to plot a zoom in of the original peak shaving to clarify more
% the details
% indexOfInterest = find(PgridN11 >= P_threshold); % the peak periodindexOfInterest = find(PgridN11 >= P_threshold); % the peak periodindexOfInterest1 = find(Pgrid >= P_threshold); % the peak period
axes('position',[.55 .155 .29 .25]);
box on % put box around new pair of axes
plot(tg(indexOfInterest),Pgrid(indexOfInterest),'b','linewidth',1.9);hold on;
plot(tg(indexOfInterest),PgridN11(indexOfInterest),'-.k','linewidth',1.5);hold on;grid on% plot on new axes% plot on new axes
legend('LF after Shaving','Orinignal LF');
axis tight
  6 Comments
Rik
Rik on 12 May 2022
Comments posted as flag by @Hossam Mosbah:
I am trying to color the two areas under PgridN11 and Pgrid as shown in the figure and every time I do that, I get overlapping colors in the two-areas
You can now copy and paste the code and it will run. the problem is in the indexofinterest.
yes sir I corrected it. sorry about that. ttt=aaaa(:,1)

Sign in to comment.

Accepted Answer

KSSV
KSSV on 12 May 2022
load('aaaa.mat') ;
time = aaaa(:,1) ;
ttt=(time/60); % Set the time in minutes for figures % Rescale the time to look good in figures
tss=ttt/60; % Convert time to hours
%% tg is the important x-axis represents hours
tg = hours(tss) + minutes(0); % x Convert time to hours
tg.Format = 'hh:mm'; % Convert time to hours format
% These are three variables represent y-axis
PgridN11=1e6*aaaa(:,2); % y2 the original load forecast without shaving
Pshave=1e6*aaaa(:,3); % y3 the threshold parameter
Pgrid = Pshave ;
Pgrid(PgridN11 >= Pshave) = 0.99*PgridN11(PgridN11 >= Pshave) ;
% Plot the threshold with original load and load after shaving
tg = (1:288)' ;
figure(1)
set(gcf,'color','w');
plot(tg,Pgrid,'b','linewidth',1.9); hold on; % tg 'x' y1 'PLShave'
plot(tg,PgridN11,'-.k','linewidth',1.7); hold on; % y2 'PgridN11'
plot(tg,Pshave,'-.r','linewidth',1.5); % y3 'Pshave'
idx = find(PgridN11 >= Pshave) ;
P = [tg(idx) PgridN11(idx) ; tg(idx(end)) PgridN11(idx(end)) ; tg(idx(1)) PgridN11(idx(1))] ;
patch(P(:,1),P(:,2),'y')
idx = find(Pgrid >= Pshave) ;
P = [tg(idx) Pgrid(idx) ; tg(idx(end)) Pgrid(idx(end)) ; tg(idx(1)) Pgrid(idx(1))] ;
patch(P(:,1),P(:,2),'r')
% the peak period
xlim([tg(1) tg(end)])
xlabel('time (hh:mm)');
ylabel('LoadForecating LF (W)');
grid on;
% xline(tg(indexOfInterest(1)),'-.b','LineWidth',1.5)
% xline(tg(indexOfInterest(end)),'-.b','LineWidth',1.5)
title('LoadForecating LF (W)');
legend('LF after Shaving','Orinignal LF','Threshold level');
stop
%%
% this part to plot a zoom in of the original peak shaving to clarify more
% the details
% indexOfInterest = find(PgridN11 >= P_threshold); % the peak periodindexOfInterest = find(PgridN11 >= P_threshold); % the peak periodindexOfInterest1 = find(Pgrid >= P_threshold); % the peak period
axes('position',[.55 .155 .29 .25]);
box on % put box around new pair of axes
plot(tg(indexOfInterest),Pgrid(indexOfInterest),'b','linewidth',1.9);hold on;
plot(tg(indexOfInterest),PgridN11(indexOfInterest),'-.k','linewidth',1.5);hold on;grid on% plot on new axes% plot on new axes
legend('LF after Shaving','Orinignal LF');
axis tight
  2 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by