plotting from a loop using timeranges
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Background:
time series for temp and chlorophyll from 2006 to current
created mean and std for each year within the timerange - will need for future anomolies
created mean and std for each month within the timerange.
Everything checks out for both temp and Chlorophyll plots. I even got the error bars to work...looks overwhelming so will work on shading error bars next...
lots of coding for each timerange so I looped the coding
Would like to plot the following within a loop
??? Not sure where these dataranges come into play and how to loop the plot as well
Thanks! SD
% extract date, total chla, 3µm chla, 1 to 3 µm chla, > 3 µm chla
datepst = OIChl.DATEPST; % date sampled
dateyear = datetime(datepst,"Format",'yyyy'); % date sampled %% this is for the time range
dateyear_1 = string(dateyear,"yyyy"); % used for the string for grouping variables
dateyear_2 = datetime(dateyear_1,'InputFormat','yyyy'); % no clue but mean's don't work wo it
datemonth = datetime(datepst,"Format",'MMM'); %this is for the time range
datemonth_1 = string(datemonth,"MMM"); % used for the string for grouping variables
datemonth_2 = datetime(datemonth_1,'InputFormat','MMM'); % no clue but mean's don't work wo it
sampletemp = OIChl.Sample_Temp; % deg C
chlaTotal = OIChl.TChla; % µg/L
chla3um = OIChl.Chla3um;% µg/L
chla3um1 = OIChl.Chla3um1; % 3µm>Chla percent(%)
chla3um2 = OIChl.Chla3um2; % 3µm<Chla percent(%)
x0=10;
y0=10;
width=1100;
height=420;
set(gcf,'position',[x0,y0,width,height]);
OI_table1 = timetable(datepst,dateyear,dateyear_1,dateyear_2,datemonth,datemonth_1,datemonth_2,sampletemp,chlaTotal,chla3um,chla3um1,chla3um2);
%Define the ranges
ranges = {timerange('2006-01-01', '2023-12-31'), ...
timerange('2014-01-01', '2014-12-31'), ...
timerange('2015-01-01', '2015-12-31'), ...
timerange('2016-01-01', '2016-12-31'), ...
timerange('2017-01-01', '2023-12-31'), ...
timerange('2024-01-01', 'now')}; % does not show for year means
Datarange ?
% Initialize a cell array to store the results
dataranges = cell(size(ranges));
% Loop through the ranges
for i = 1:numel(ranges)
% Extract the data for the current range
TRange = OI_table1(ranges{i}, :);
% Calculate the mean and standard deviation for each year
tempValuesYear = varfun(@(x) [mean(x,'omitnan'), std(x,'omitnan')], TRange,'GroupingVariables',{'dateyear_2'},'InputVariables',{'sampletemp','chlaTotal','chla3um','chla3um1','chla3um2'}); % year mean over certain ranges specified
% split and rename the mean and standard deviation for each year
tempMeanYear = splitvars(tempValuesYear,{'Fun_sampletemp','Fun_chlaTotal','Fun_chla3um','Fun_chla3um1','Fun_chla3um2'}, 'NewVariableNames',{{'mean_sampletemp','std_sampletemp'},{'mean_chlaTotal','std_chlaTotal'},{'mean_chla3um','std_chla3um'},{'mean_chla3um1','std_chla3um1'},{'mean_chla3um2','std_chla3um2'}});
technically don't need year means but will for anomaly plotting
% Calculate the mean and standard deviation for every month withing given range
tempValuesMonth = varfun(@(x) [mean(x,'omitnan'), std(x,'omitnan')], TRange,'GroupingVariables',{'datemonth_2'},'InputVariables',{'sampletemp','chlaTotal','chla3um','chla3um1','chla3um2'}); % month mean over certain year ranges specified
% split and rename the mean and standard deviation for every month withing given range
tempMeanMonth = splitvars(tempValuesMonth, {'Fun_sampletemp','Fun_chlaTotal','Fun_chla3um','Fun_chla3um1','Fun_chla3um2'}, 'NewVariableNames',{{'mean_sampletemp','std_sampletemp'},{'mean_chlaTotal','std_chlaTotal'},{'mean_chla3um','std_chla3um'},{'mean_chla3um1','std_chla3um1'},{'mean_chla3um2','std_chla3um2'}});
datarange ?
% Store the results in the cell array
dataranges{i} = dataranges;
% Plot the data
figure;
plot(dataranges, 'datemonth_2', 'mean_sampletemp')
can plot using tempMeanMonth table but that does not help with the loop
hold on
When I did not loop, I was using tempMeanMonth1, tempMeanMonth2,....tempMeanMonth6 and the plots layed down like magic. also plotted for Chlorophyll and then got errorbars. Once I looped, no plot!
%plot(tempMeanMonth2, 'datemonth_2', 'mean_sampletemp') %% code pre loop and worked
%plot(tempMeanMonth3, 'datemonth_2', 'mean_sampletemp')
%plot(tempMeanMonth4, 'datemonth_2', 'mean_sampletemp')
%plot(tempMeanMonth5, 'datemonth_2', 'mean_sampletemp')
%plot(tempMeanMonth6, 'datemonth_2', 'mean_sampletemp')
hold off
datetick('x','mmm','keeplimits','keepticks')
title('Temperature Monthly Means')
xlabel('Date (Months)')
%set(gca,'xTickLabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})
ylabel('Temperature (^oC)')
ylim([12 25])
legend('2006 - 2013','2014 - 2014','2015 - 2015','2016 - 2016','2017 - 2023','2024 to current','Orientation','v',"Location","eastoutside")
end
0 Kommentare
Antworten (1)
Umar
am 8 Jul. 2024
Hi SD,
To successfully plot temperature monthly means for different date ranges within a loop, you need to ensure that the data extraction, calculation of means, and plotting steps are correctly implemented.
Here's a step-by-step guide to help you achieve this:
1. Data Extraction and Calculation: - Define the date ranges using timerange objects for each period of interest. - Loop through these date ranges and extract the relevant data from your OI_table1 timetable using the ranges. - Calculate the mean and standard deviation for temperature and chlorophyll variables for each year and month within the specified date ranges.
2. Plotting in a Loop: - Initialize a cell array to store the results for each date range. - Within the loop, plot the temperature monthly means using the tempMeanMonth table generated for each range. - Ensure that you use the correct data variables and format them appropriately for plotting. - You can use 'hold on' to overlay multiple plots within the same figure if needed.
3. Additional Tips: - Make sure that the data is structured correctly for plotting by verifying column names and data types. - Check that all necessary variables are included in the plotting function call. - Adjust plot settings such as labels, legends, and axis limits to enhance visualization.
By following these steps and making necessary adjustments to your code, you should be able to successfully plot temperature monthly means for different date ranges within a loop.
If you encounter any specific errors or issues during implementation, feel free to provide more details for further assistance. I hope this guidance helps you resolve your plotting challenge efficiently. If you have any more questions or need additional clarification, please don't hesitate to ask. Good luck with your data visualization efforts!
0 Kommentare
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!