How to make a simple plot with two lines?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Macy
el 9 de Feb. de 2023
Respondida: Venkat Siddarth
el 9 de Feb. de 2023
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(:)
% 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.
0 comentarios
Respuesta aceptada
Paul
el 9 de Feb. de 2023
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(:)
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')
0 comentarios
Más respuestas (2)
Venkat Siddarth
el 9 de Feb. de 2023
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
0 comentarios
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!