plotting 12 graphs in one figure

조회 수: 7 (최근 30일)
Michelangelo Cannistraro
Michelangelo Cannistraro 2022년 12월 1일
답변: MarKf 2022년 12월 1일
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
This script plots 12 graphs with the mean, max,min, temperature of every month. this needs to be in a subplot and all the months needs to be on one figure. Also the mean,max,min need to be in their own figure. So it has to be three figures with 12 graphs in each figure, one for mean, one for min, and one for max. (I added the files to get the cleanup variable)

채택된 답변

Voss
Voss 2022년 12월 1일
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'};
figure
sgtitle('Monthly Data : Avg')
for ck = 1:12
subplot(3,4,ck)
plot(x,mo_av(:,ck),'-*');
xlabel('Years')
title(str_mo(ck));
end
figure
sgtitle('Monthly Data : Max')
for ck = 1:12
subplot(3,4,ck)
plot(x,mo_max(:,ck),'-*');
xlabel('Years')
title(str_mo(ck));
end
figure
sgtitle('Monthly Data : Min')
for ck = 1:12
subplot(3,4,ck)
plot(x,mo_min(:,ck),'-*');
xlabel('Years')
title(str_mo(ck));
end

추가 답변 (1개)

MarKf
MarKf 2022년 12월 1일
So instead of plottin 12 figures each with mo_av, mo_max and mo_min you want 3 with 12 subplots? Would subplot(1,12,x) do? that is a long 12 subplots figure (then I suggest to increase the figure dimentions with another parameter-value pair when calling figure, like figure(...'Pos',[...])) otherwise you can do something like subplot(3,4,x).
% plot montly data
str_me = {'Avg','Max','Min'};
str_mo = {'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
for ck = 1:3
figure('Name',str_me{ck})
switch ck, case 1, mo = mo_av; case 2, mo = mo_max; case 3, mo = mo_min; end
for smi = 1:12
subplot(1,12,smi)
plot(x,mo(:,smi),'-*');
if smi == 6, xlabel('Years'), end
title(str_mo(smi));
end
end

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by