Boxplot and mean for selected range of temporal datasets
Show older comments
Hello there,
I have a datasets containing 3 parameters, let's say z_i as depth, HOUR as time (in numeric format), and EP as the parameter I want to check its variability over depth and time. My intention is to get the EP median, min-max, and mean for selected range of z_i for each time, and then plot them all in a box plot and mean (in one plot), with x-axis as time and y-plot is the EP (better plotted in log-10 scale as the difference is very small).
See my file attached and the illustration of the flow process as I described above below:

Thank you!
1 Comment
Mathieu NOE
on 5 Nov 2024
hello
have you started something ? what issue are you facing ?
Answers (1)
Shishir Reddy
on 6 Nov 2024
Hi Adi
As per my understanding, you would like to filter a dataset based on a specified depth range (‘z_i’) and calculate statistical measures (median, min, max, mean) of the parameter ‘EP’ for each hour (‘HOUR’).
From the ‘data_w.mat’ file, I see that ‘EP’ and ‘HOUR’ are matrices with 6 rows (possibly representing different observations or repetitions) and 9991 columns , while ‘z_i’ is a vector with 9991 elements, representing depth.
You can process this data in MATLAB as follows –
1. Load the data
load('data_w.mat'); % This loads EP, HOUR, and z_i
2. Filter the data
filtered_indices = (z_i >= z_min) & (z_i <= z_max); % z_min, z_max, depends on the selected range
filtered_EP = EP(:, filtered_indices);
filtered_HOUR = HOUR(:, filtered_indices);
filtered_EP = filtered_EP(:);% Flattening the matrices
filtered_HOUR = filtered_HOUR(:);
3. Compute the statistics
% Assumption – ‘unique_hours’, ‘medians’, ‘mins’, ‘maxs’, ‘means’ are declared.
for i = 1:length(unique_hours)
hour_data = filtered_EP(filtered_HOUR == unique_hours(i));
medians(i) = median(hour_data);
mins(i) = min(hour_data);
maxs(i) = max(hour_data);
means(i) = mean(hour_data);
end
4. Plotting
figure;
hold on;
boxplot(filtered_EP, filtered_HOUR, 'Colors', [0.7 0.7 0.7], 'Symbol', '');
plot(unique_hours + 1, means, 'ro-', 'LineWidth', 1.5, 'DisplayName', 'Mean');
set(gca, 'YScale', 'log');
For more information regarding the ‘boxplot’ function, kindly refer to the following documentation - https://www.mathworks.com/help/stats/boxplot.html
I hope this helps.
8 Comments
Adi Purwandana
on 6 Nov 2024
Edited: Adi Purwandana
on 6 Nov 2024
load('data_w.mat'); % This loads EP, HOUR, and z_i
z_min = min(z_i);
z_max = max(z_i);
filtered_indices = (z_i >= z_min) & (z_i <= z_max); % z_min, z_max, depends on the selected range
filtered_EP = EP(:, filtered_indices);
filtered_HOUR = HOUR(:, filtered_indices);
filtered_EP = filtered_EP(:);% Flattening the matrices
filtered_HOUR = filtered_HOUR(:);
unique_hours = unique(filtered_HOUR);
unique_hours(isnan(unique_hours)) = [];
NH = numel(unique_hours);
medians = zeros(1,NH);
mins = zeros(1,NH);
maxs = zeros(1,NH);
means = zeros(1,NH);
exp_log_means = zeros(1,NH);
for i = 1:NH
hour_data = filtered_EP(filtered_HOUR == unique_hours(i));
medians(i) = median(hour_data);
mins(i) = min(hour_data);
maxs(i) = max(hour_data);
means(i) = mean(hour_data,'omitnan');
exp_log_means(i) = exp(mean(log(hour_data),'omitnan'));
end
figure;
hold on;
boxplot(filtered_EP, filtered_HOUR);
plot(1:NH, means, 'go-', 'LineWidth', 1.5);
plot(1:NH, exp_log_means, 'ko-', 'LineWidth', 1.5);
set(gca, 'YScale', 'log');
[hh,mm,ss] = hms(hours(unique_hours));
xticklabels(string(datetime(0,0,0,hh,mm,round(ss),'Format','HH:mm:ss')))
Adi Purwandana
on 6 Nov 2024
Edited: Adi Purwandana
on 6 Nov 2024
Voss
on 6 Nov 2024
Maybe something like this at the end, assuming you meant x-axis:
[hh,mm,ss] = hms(hours(unique_hours));
xticklabels(string(datetime(0,0,0,hh,mm,round(ss),'Format','HH:mm:ss')))
The zeros in the datetime call represent year, month, day; if your hour data (~40725 hours) represent an offset from some date, you can include that information instead of using zeros, and modify the 'Format' accordingly. See datetime.
(I've modifed my previous comment to include these lines of code to show the effect.)
Adi Purwandana
on 6 Nov 2024
Edited: Adi Purwandana
on 7 Nov 2024
Adi Purwandana
on 7 Nov 2024
Adi Purwandana
on 7 Nov 2024
Voss
on 7 Nov 2024
Ah, so HOUR is actually days.
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
