Creating a specific time subplot

6 ビュー (過去 30 日間)
Rachel Cox
Rachel Cox 2022 年 11 月 28 日
回答済み: Seth Furman 2022 年 11 月 28 日
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

採用された回答

VBBV
VBBV 2022 年 11 月 28 日
%hold on. Comment this line
subplot(2,1,2)
Comment the line above
  1 件のコメント
Rachel Cox
Rachel Cox 2022 年 11 月 28 日
I'm unsure what this means

サインインしてコメントする。

その他の回答 (1 件)

Seth Furman
Seth Furman 2022 年 11 月 28 日
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"]);

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by