Creating a specific time subplot

8 views (last 30 days)
Rachel Cox
Rachel Cox on 28 Nov 2022
Answered: Seth Furman on 28 Nov 2022
Hello
Im plotting time and wave height and im trying to plot a graph shpwing yearly trends but highlighting February and march and then a subplot of just february and march. So far I have highlighted the two months in different olours (trying to make them both red) and my subplot just shows march.
figure(1)
for m=1:12
V.H.Time = data{1,m}{1,1}(:,1);
V.H.Hm0 = data{1,m}{1,2}(:,6);
subplot(2,1,1)
plot(V.H.Time,V.H.Hm0)
hold on
plot(V.H.Time,V.H.Hm0, 'k') %keep one figure open and add extra data open (stops multiple figures)
hold on
m=4
V.H.Time = data{1,m}{1,1}(:,1);
V.H.Hm0 = data{1,m}{1,2}(:,6);
plot(V.H.Time,V.H.Hm0)
m=8
V.H.Time = data{1,m}{1,1}(:,1);
V.H.Hm0 = data{1,m}{1,2}(:,6);
plot(V.H.Time,V.H.Hm0)
plot(V.H.Time,V.H.Hm0, 'r')
datetick('x','mmm','keeplimits')
hold on
subplot(2,1,2)
m=4
plot(V.H.Time,V.H.Hm0)
m=8
plot(V.H.Time,V.H.Hm0)
hold on
plot(V.H.Time,V.H.Hm0, 'r')
datetick('x','mmm','keeplimits')
end

Accepted Answer

VBBV
VBBV on 28 Nov 2022
%hold on. Comment this line
subplot(2,1,2)
Comment the line above

More Answers (1)

Seth Furman
Seth Furman on 28 Nov 2022
To color each month, you can use stackedplot and provide multiple timetables
tt = timetable(datetime(2022,1,1)+hours(0:24*365-1)',cos(linspace(0,2*pi,24*365)'+randn(24*365,1)/10))
tt = 8760×1 timetable
Time Var1 ____________________ _______ 01-Jan-2022 00:00:00 0.9978 01-Jan-2022 01:00:00 0.99909 01-Jan-2022 02:00:00 0.98911 01-Jan-2022 03:00:00 0.98734 01-Jan-2022 04:00:00 0.99889 01-Jan-2022 05:00:00 0.9869 01-Jan-2022 06:00:00 0.99972 01-Jan-2022 07:00:00 0.98416 01-Jan-2022 08:00:00 0.95042 01-Jan-2022 09:00:00 0.99574 01-Jan-2022 10:00:00 0.99892 01-Jan-2022 11:00:00 0.9827 01-Jan-2022 12:00:00 0.99661 01-Jan-2022 13:00:00 0.99449 01-Jan-2022 14:00:00 0.99796 01-Jan-2022 15:00:00 0.99166
janToMarch = tt(1 <= tt.Time.Month & tt.Time.Month <= 3,:);
aprilToJune = tt(4 <= tt.Time.Month & tt.Time.Month <= 6,:);
sp = stackedplot(janToMarch, aprilToJune);
then use colororder
colororder(sp, ["black","#EDB120"]);
To create multiple subplots, you can use tiledlayout
months = cell(1,12);
for i = 1:numel(months)
months{i} = tt(tt.Time.Month == i,:);
end
tl = tiledlayout(2,1);
nexttile;
stackedplot(months, LegendVisible="off");
nexttile;
stackedplot(months(2:3), LegendLabels=["February","March"]);

Tags

Community Treasure Hunt

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

Start Hunting!