How to move forward the shaded area in MATLAB plot?
1 view (last 30 days)
Show older comments
Armando MAROZZI
on 24 Mar 2021
Answered: Ananya Tewari
on 31 Mar 2021
I need to plot a graph where the first line is from, say, 2006-2008, and the second one from 2008-2009. The latter one has confidence interval which I plot as shaded area but the result is the one you see in the plot attached. How can I get the shaded area to move forward and align with the solid red line?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/560583/image.jpeg)
The code I have is the following:
x = (1:size(forecast,1)).';
plot(t(t >= '01-Jan-2006' & t < '01-Jan-2008'),movmean(dep_b,30),'-k',t(t >= '01-Jan-2008' & t <= '02-Nov-2009'), movmean(forecast(1:length(forecast)),30),'-r')
hold on
patch([x; flipud(x)], [movmean(forecast(:,3),30); flipud(movmean(forecast(:,2),30))], [1,0,0], 'FaceAlpha', .1, 'EdgeColor', 'none');
hold on
xline(t(t == '01-Jan-2008'), '-b')
legend('Actual', 'Forecast', 'Orientation', 'horizontal', 'Location', 'southoutside')
box on;
How can I modify it to shift the shaded area forward?
Thanks a lot!
0 Comments
Accepted Answer
Ananya Tewari
on 31 Mar 2021
I understand you want to shift the shaded area forward and align it with the forecasted plot. Applying the offset in the x-coordinates of the patch() can help get the dersired result. The offset should be following the unit of x
patch([x+offset; flipud(x)+offset], [movmean(forecast(:,3),30); flipud(movmean(forecast(:,2),30))], [1,0,0], 'FaceAlpha', .1, 'EdgeColor', 'none');
0 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!