How to make a line-plot? Time series
1 view (last 30 days)
Show older comments
Hi there, simple question. This is a subset of a larger dataset. How do I make a lineplot of the schoolyear 2000 and 2002 "students" time series? It should be one plot with two lines.
Thank you.
clc;
close all;
clear all;
table_a = readtable('Data1.xlsx');
for i = 1:height(table_a)
if table_a.month(i)>=8
schoolyear(i) = table_a.year(i) + 1;
else
schoolyear(i) = table_a.year(i);
end
end
table_a.schoolyear = schoolyear(:)
2 Comments
Les Beckham
on 8 Feb 2023
What did you try?
You probably need to decide what you want to plot the selected students data against. In other words, if the selected students data is to be on the y axis of the plot, what goes on the x axis?
Accepted Answer
Les Beckham
on 9 Feb 2023
Edited: Les Beckham
on 9 Feb 2023
Maybe this is what you are looking for. This isn't very pretty using the small sample dataset but maybe it will look better with real data.
table_a = readtable('Data1.xlsx');
% change a few of the months in 2001 to less than 8 so they are in schoolyear 2001
table_a.month(4:6) = 2:4;
% it is a good practice to preallocate before filling the vector in a loop
schoolyear = zeros(size(table_a.month));
for i = 1:height(table_a)
if table_a.month(i) >= 8
schoolyear(i) = table_a.year(i) + 1;
else
schoolyear(i) = table_a.year(i);
end
end
table_a.schoolyear = schoolyear(:);
% create a datetime column to plot against (as the x axis data)
table_a.date = datetime(table_a.year, table_a.month, table_a.day)
idx = (table_a.schoolyear == 2000) | (table_a.schoolyear == 2002);
plot(table_a.date(idx), table_a.students(idx), 'o'); % I think it looks better without the lines
xlabel 'Date'
ylabel 'Student count'
grid on
An alternative would be to put each selected year in a subplot or tile.
desired_schoolyears = [2000 2002];
figure
tiledlayout(numel(desired_schoolyears), 1)
for i = 1:numel(desired_schoolyears)
nexttile
idx = table_a.schoolyear == desired_schoolyears(i);
plot(table_a.date(idx), table_a.students(idx));
xlabel 'Date'
ylabel 'Student count'
title(sprintf('School Year %d', desired_schoolyears(i)))
grid on
end
EDIT:
It actually might be more natural to put the subplots side-by-side and give them a common y axis scaling for easier comparison of the data.
figure
tiledlayout(1, numel(desired_schoolyears), 'TileSpacing', 'tight')
yl = zeros(numel(desired_schoolyears), 2);
for i = 1:numel(desired_schoolyears)
nexttile
idx = table_a.schoolyear == desired_schoolyears(i);
plot(table_a.date(idx), table_a.students(idx));
xlabel 'Date'
ylabel 'Student count'
title(sprintf('School Year %d', desired_schoolyears(i)))
grid on
yl(i,:) = ylim;
end
% adjust the y axis limits to match
ymin = min(yl(:,1));
ymax = max(yl(:,2));
nexttile(1)
ylim([ymin, ymax]);
nexttile(2)
ylim([ymin, ymax]);
2 Comments
More Answers (1)
Sulaymon Eshkabilov
on 8 Feb 2023
Here is one of the possible solution codes:
table_a = readtable('Data1.xlsx');
table_a.schoolyear = table_a.year;
table_a.schoolyear(table_a.month>=8) = table_a.year(table_a.month>=8)+1;
figure
yyaxis left
plot(1:height(table_a),table_a.students, 'bo-', 'LineWidth',2)
ylabel('# of students')
ylim([min(table_a.students)-1, max(table_a.students)+1])
yyaxis right
plot(1:height(table_a), table_a.schoolyear, 'rd--', 'LineWidth',2)
yticks([ 2000 2001 2002 ])
yticklabels({ '2000','2001','2002' })
ylim([1999 2003])
ylabel('School year')
grid on
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!