plotting 12 graphs for different months
조회 수: 1 (최근 30일)
이전 댓글 표시
c=1;
for i=1975:2016
for j = 1:12
monthdata(c)=mean(cleanup(4,cleanup(1,:)==i & cleanup(2,:)==j));
c=c+1;
end
end
this script makes a 1x504 matrix for the monthdata variable. this contains the mean of the temperatures of each month of the cleanup variable (made by the links). I must now take the mean of every month and plot each month individually. i must create 12 graphs representing the mean of every month but I do not know how to get thos values out of the 1x504 matrix or how to write the script that will take and plot each months mean for the cleanup variabe with all the temperatures for each day and month.
댓글 수: 0
답변 (1개)
Mathieu NOE
2022년 12월 1일
hello
I tweaked a bit your code
load data_matrix.mat
data=SECTION_L;
data(:,1:3)=[];
row=9;
cleanup=data([1 2 3 row],:);
% year range
y1 = 1975;
y2 = 2016;
% init
x = y1:y2;
nx = numel(x);
mo_av = zeros(nx,12);
mo_max = zeros(nx,12);
mo_min = zeros(nx,12);
max_day = zeros(nx,12);
min_day = zeros(nx,12);
k = 0;
for k = 1:nx % year loop
col_dat_yr=find(cleanup(1,:)== x(k)); % find data corresponding to year = x(k)
data_yr=cleanup(:,col_dat_yr);
for mmonth = 1:12
col_dat_mo=find(data_yr(2,:)==mmonth);
data_mo=data_yr(:,col_dat_mo);
mo_av(k,mmonth)=mean(data_mo(4,:));
mo_max(k,mmonth)=max(data_mo(4,:));
mo_min(k,mmonth)=min(data_mo(4,:));
max_day(k,mmonth)=find(data_mo(4,:)==mo_max(k,mmonth));
min_day(k,mmonth)=find(data_mo(4,:)==mo_min(k,mmonth));
end
end
% plot montly data
str_mo = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
for ck = 1:12
figure(ck)
plot(x,mo_av(:,ck),'-*',x,mo_max(:,ck),'-*',x,mo_min(:,ck),'-*');
xlabel('Years')
title([' Monthly Data : ' str_mo(ck)]);
legend('Avg','Max','Min');
end
댓글 수: 4
Mathieu NOE
2022년 12월 2일
hello
ok but making 12 subplots in one figure will make them barely readable
or you want 12 organized in 6 rows / 2 columns or 4 rows / 3 columns or ??
below suggestion with 4 x 3 plot
load data_matrix.mat
data=SECTION_L;
data(:,1:3)=[];
row=9;
cleanup=data([1 2 3 row],:);
% year range
y1 = 1975;
y2 = 2016;
% init
x = y1:y2;
nx = numel(x);
mo_av = zeros(nx,12);
mo_max = zeros(nx,12);
mo_min = zeros(nx,12);
max_day = zeros(nx,12);
min_day = zeros(nx,12);
k = 0;
for k = 1:nx % year loop
col_dat_yr=find(cleanup(1,:)== x(k)); % find data corresponding to year = x(k)
data_yr=cleanup(:,col_dat_yr);
for mmonth = 1:12
col_dat_mo=find(data_yr(2,:)==mmonth);
data_mo=data_yr(:,col_dat_mo);
mo_av(k,mmonth)=mean(data_mo(4,:));
mo_max(k,mmonth)=max(data_mo(4,:));
mo_min(k,mmonth)=min(data_mo(4,:));
max_day(k,mmonth)=find(data_mo(4,:)==mo_max(k,mmonth));
min_day(k,mmonth)=find(data_mo(4,:)==mo_min(k,mmonth));
end
end
% plot montly data
str_mo = {'Jan'; 'Feb'; 'Mar'; 'Apr'; 'May'; 'Jun'; 'Jul'; 'Aug'; 'Sep'; 'Oct'; 'Nov'; 'Dec'};
% plot "avg"
t1 = tiledlayout(4,3,'TileSpacing','Compact');
for ck = 1:12
nexttile
p1 = plot(x,mo_av(:,ck),'LineWidth',2);
title(str_mo(ck))
end
% Display a shared title and axis labels by passing t to the title, xlabel, and ylabel functions.
title(t1,'Monthly Avg')
xlabel(t1,'Years')
ylabel(t1,'Temperature')
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!