How to make a simple plot with two lines?

2 次查看(过去 30 天)
Hi. I am new to MatLab. I want to make a plot with two lines for 2001 and 2003 group of schoolyear vs students. (Please note, not "year", but "schoolyear"). The x-axis would have the 12 months of the year, and the y-axis has the students.
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 = 48×4 table
month year students schoolyear _____ ____ ________ __________ 1 2000 12 2000 2 2000 14 2000 3 2000 13 2000 4 2000 11 2000 5 2000 17 2000 6 2000 14 2000 7 2000 10 2000 8 2000 9 2001 9 2000 16 2001 10 2000 11 2001 11 2000 4 2001 12 2000 6 2001 1 2001 5 2001 2 2001 7 2001 3 2001 8 2001 4 2001 9 2001
% how do I make a single plot with two lines, one for 2001 and one for
% 2003. It would be a time series, the month would be on the x-axis, and
% students would be on the y-axis.

采纳的回答

Paul
Paul 2023-2-9
Hi Macy,
%table_a = readtable('Data1.xlsx');
table_a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1289895/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 = 48×4 table
month year students schoolyear _____ ____ ________ __________ 1 2000 12 2000 2 2000 14 2000 3 2000 13 2000 4 2000 11 2000 5 2000 17 2000 6 2000 14 2000 7 2000 10 2000 8 2000 9 2001 9 2000 16 2001 10 2000 11 2001 11 2000 4 2001 12 2000 6 2001 1 2001 5 2001 2 2001 7 2001 3 2001 8 2001 4 2001 9 2001
Logical indices for the desired school years.
index2001 = table_a.schoolyear == 2001;
index2003 = table_a.schoolyear == 2003;
Extract the corresponding months and students.
month2001 = table_a.month(index2001); students2001 = table_a.students(index2001);
month2003 = table_a.month(index2003); students2003 = table_a.students(index2003);
Sort by month in ascending order (I assumed this is what you want). Reorder corresponding students.
[month2001,index2001] = sort(month2001); students2001 = students2001(index2001);
[month2003,index2003] = sort(month2003); students2003 = students2003(index2003);
Plot
plot(month2001,students2001,'-o',month2003,students2003,'-o');
xlabel('month'),ylabel('students')
legend('2001','2003')

更多回答(2 个)

Venkat Siddarth
Venkat Siddarth 2023-2-9
Hi @Macy,
I understand that you are trying to plot two lines in a single axis with x-axis as months and y-axis as corresponding students in the respective years. This can be achieved as follows:
%Firstly,load the data and add the "schoolyear" column
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(:)
After that we will obtain the data in the form of vector using curly brackets({})
%obtaining and sorting months and corresponding students data with schoolyear 2001
data_2001=sortrows(table_a{table_a{:,"schoolyear"}==2001,["month" "students"]},1);
%obtaining and sorting months and corresponding students data with schoolyear 2003
data_2003=sortrows(table_a{table_a{:,"schoolyear"}==2003,["month" "students"]},1);
After that we plot the data using plot function and using the command hold on we create the plots on same axis.
plot(data_2001(:,1),data_2001(:,2),"s-");
hold on
plot(data_2003(:,1),data_2003(:,2),"o-");
legend(["2001" "2003"])
hold off
I hope this resolves your query.
Thanks,
Venkat Siddarth V

Macy
Macy 2023-2-9
@Paul thank you, this saved me a lot of time as I was trying to do it using a for loop.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by