Clear Filters
Clear Filters

Plot several time series data stacked in one graph

9 views (last 30 days)
Dear all,
I have the dynamic area data for 7 years from 2016 to 2022 as attached in the excel file. I want to plot 7 line of data with x axis is the date from 01/01 to 31/12 (year excluded) and y axis is the area value but I do not know how to plot with only day and month. Then I want to add a legend to distinguish 7 years. Can you help me with this please!

Accepted Answer

Star Strider
Star Strider on 5 Mar 2023
Edited: Star Strider on 5 Mar 2023
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1314900/Data.xlsx')
T1 = 61×14 table
Date Area Date_1 Area_1 Date_2 Area_2 Date_3 Area_3 Date_4 Area_4 Date_5 Area_5 Date_6 Area_6 ___________ ______ ___________ ______ ___________ ______ ___________ ______ ___________ ______ ___________ ______ ___________ ______ 05-Jan-2016 158.65 11-Jan-2017 168.09 02-Jan-2018 166.72 01-Jan-2019 169.81 04-Jan-2020 188.97 02-Jan-2021 160.21 05-Jan-2022 185.54 29-Jan-2016 152.5 23-Jan-2017 185.95 06-Jan-2018 169.31 09-Jan-2019 175.72 16-Jan-2020 188.12 10-Jan-2021 184.51 09-Jan-2022 181.51 29-Jan-2016 152.22 04-Feb-2017 139.63 14-Jan-2018 183.79 13-Jan-2019 168.49 20-Jan-2020 167.2 14-Jan-2021 185.47 17-Jan-2022 177.08 22-Feb-2016 129.21 28-Feb-2017 92.981 18-Jan-2018 172.3 21-Jan-2019 177.76 28-Jan-2020 156.87 22-Jan-2021 193.3 21-Jan-2022 182.6 17-Mar-2016 82.515 12-Mar-2017 69.727 26-Jan-2018 182.32 25-Jan-2019 170.04 01-Feb-2020 149.89 26-Jan-2021 183.29 29-Jan-2022 180.8 29-Mar-2016 52.889 20-Mar-2017 70.524 30-Jan-2018 176.93 02-Feb-2019 165.22 09-Feb-2020 160.9 03-Feb-2021 192.29 02-Feb-2022 156.36 10-Apr-2016 30.371 24-Mar-2017 46.262 07-Feb-2018 185.58 06-Feb-2019 106.3 13-Feb-2020 116.12 07-Feb-2021 175.68 10-Feb-2022 168.25 22-Apr-2016 35.104 01-Apr-2017 72.123 11-Feb-2018 168.06 14-Feb-2019 147.81 21-Feb-2020 131.22 15-Feb-2021 167.87 14-Feb-2022 143.91 04-May-2016 32.536 05-Apr-2017 51.857 19-Feb-2018 142.79 18-Feb-2019 119.44 25-Feb-2020 102.37 19-Feb-2021 163.13 26-Feb-2022 122.97 16-May-2016 22.553 13-Apr-2017 80.741 23-Feb-2018 143.75 26-Feb-2019 130.1 04-Mar-2020 107.98 27-Feb-2021 146.14 06-Mar-2022 121.46 28-May-2016 24.086 17-Apr-2017 53.832 03-Mar-2018 120.34 02-Mar-2019 83.683 08-Mar-2020 70.597 03-Mar-2021 127.35 10-Mar-2022 96.215 09-Jun-2016 51.881 25-Apr-2017 60.137 07-Mar-2018 80.377 10-Mar-2019 94.3 16-Mar-2020 102.43 11-Mar-2021 116.37 18-Mar-2022 72.044 03-Jul-2016 40.675 29-Apr-2017 31.755 15-Mar-2018 146.22 14-Mar-2019 60.514 20-Mar-2020 81.31 15-Mar-2021 87.935 22-Mar-2022 58.319 15-Jul-2016 34.905 07-May-2017 52.026 19-Mar-2018 53.703 22-Mar-2019 68.938 28-Mar-2020 81.958 23-Mar-2021 114.61 30-Mar-2022 65.384 27-Jul-2016 34.465 11-May-2017 25.498 27-Mar-2018 69.204 26-Mar-2019 68.091 01-Apr-2020 70.418 27-Mar-2021 48.921 03-Apr-2022 97.192 08-Aug-2016 37.957 19-May-2017 21.614 31-Mar-2018 41.826 03-Apr-2019 84.816 09-Apr-2020 82.753 04-Apr-2021 100.08 11-Apr-2022 69.909
VN = T1.Properties.VariableNames;
DateMtx = T1{:,1:2:end};
for k = 1:size(DateMtx,2)
[y,m,d] = ymd(DateMtx(:,k));
MDdt(:,k) = datetime(1,m,d);
end
figure
hold on
for k = 1:size(MDdt,2)
plot(MDdt(:,k), T1{:,2*k}, 'DisplayName',strrep(VN{2*k},'_','\_'))
end
grid
xtickformat('dd/MM')
legend('Location','best')
xlabel('dd/MM')
ylabel('Area')
EDIT — (5 Mar 2023 at 17:38)
Corrected typographical error (specifically so that xticklabel correctly matches xtickformat). Code unchanged.
.
  2 Comments
Vu Minh
Vu Minh on 6 Mar 2023
This is superb. Thank you so much sir. It works very well for me with merely modification!

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!