plotting 12 graphs for different months

2 views (last 30 days)
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.

Answers (1)

Mathieu NOE
Mathieu NOE on 1 Dec 2022
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 Comments
Michelangelo Cannistraro
Michelangelo Cannistraro on 1 Dec 2022
This gives me three figures, each figure has either the max,min,mean of each month in one graph. I would need each month to have its own graph inside the figure. So, for example, for min i would need 12 individual graphs for each month but put into one singular figure with the 12 graphs in it. Unfortunatly this scipt puts all the values of minimum of every month onto one graph inside the figure for minimum results.
Mathieu NOE
Mathieu NOE on 2 Dec 2022
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')

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!