How to make a line-plot? Time series

1 visualizzazione (ultimi 30 giorni)
Macy
Macy il 8 Feb 2023
Commentato: Les Beckham il 13 Feb 2023
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(:)
table_a = 9×5 table
month day year students schoolyear _____ ___ ____ ________ __________ 7 29 2000 12 2000 7 30 2000 14 2000 7 31 2000 13 2000 8 1 2001 11 2002 8 2 2001 17 2002 8 3 2001 14 2002 8 4 2001 10 2002 8 5 2001 9 2002 8 6 2001 16 2002
  2 Commenti
Les Beckham
Les Beckham il 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?
Macy
Macy il 9 Feb 2023
I would want the date on the x-axis, and the students on the y-axis. And since this is a representation of a larger data set, I want to know how to make a plot for a select few years. So if this data set includes 365 days per year from 1990 to 2020, I want to plot for example the year 1992, 2003, and 2006 on one plot.
So I am not sure how to do that. I have not tried any thing as I am not sure how to select for the "schoolyear" that I want.

Accedi per commentare.

Risposta accettata

Les Beckham
Les Beckham il 9 Feb 2023
Modificato: Les Beckham il 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)
table_a = 9×6 table
month day year students schoolyear date _____ ___ ____ ________ __________ ___________ 7 29 2000 12 2000 29-Jul-2000 7 30 2000 14 2000 30-Jul-2000 7 31 2000 13 2000 31-Jul-2000 2 1 2001 11 2001 01-Feb-2001 3 2 2001 17 2001 02-Mar-2001 4 3 2001 14 2001 03-Apr-2001 8 4 2001 10 2002 04-Aug-2001 8 5 2001 9 2002 05-Aug-2001 8 6 2001 16 2002 06-Aug-2001
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 Commenti
Macy
Macy il 12 Feb 2023
Thank you @Les Beckham for taking the time to answer this question!
Les Beckham
Les Beckham il 13 Feb 2023
You are quite welcome.

Accedi per commentare.

Più risposte (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 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

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by