Show plot with different "sampling" number side-by-side

1 次查看(过去 30 天)
Water samples are taken at different depths and in 10 different regions of the ocean. Lets say we do 5 casts per region. I was able to plot 50 individual plots and title them as such. Great...I am doing good but now I want to create 1 figure for each region. The numbers for casts and regions are not fixed because you know scientists and weather don't work that way. I tried to plot all the casts for the region on one figure and I chose 4. Great but now I see 4 side by side plots all with cast 2...not so great. I would like to see cast1, cast2, cast3... side-by-side. The following is using nutrient data. Should I do a loop with region = transect first and then the cast? I know the plot example below needs to be enlarged so I can see the plots nicely.
clear,close all;clc
opts = spreadsheetImportOptions("NumVariables", 25);
% Specify sheet and range
opts.Sheet = "Nutrient_QC";
opts.DataRange = "A2:Y388";
% Specify column names and types
opts.VariableNames = ["StudyName", "Transect", "Station", "SampleType", "Cast", "Bottle", "NominalDepthm", "MSInum", "StationNote", "FilterType", "NutrientAnalysisNote", "rack", "NO3", "NO2", "PO4", "SIL", "NH4", "PhosphateML002", "PhosphateMLX10", "SilicateML05", "NitriteML002", "NitriteMLX10", "NitrateML", "AmmoniaML003", "AmmoniaMLX10"];
opts.VariableTypes = ["categorical", "categorical", "categorical", "categorical", "double", "double", "double", "double", "categorical", "categorical", "categorical", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, ["StudyName", "Transect", "Station", "SampleType", "StationNote", "FilterType", "NutrientAnalysisNote"], "EmptyFieldRule", "auto");
% Import the data
NutrientDataPlots = readtable("C:\Users\myname\Desktop\Cruise Nutrients\NutrientData_Plots_QC.xlsx", opts, "UseExcel", false);
%Clear temporary variables
clear opts
% Sample data
Cast = [NutrientDataPlots.Cast]; % Cast
fig = gcf;
x0=10;
y0=10;
width=2000;
height=500;
set(gcf,'position',[x0,y0,width,height]);
% Loop through each cast and plot
uniqueCasts = unique(NutrientDataPlots.Cast, "rows","sorted");
specificTransects = unique(NutrientDataPlots.Transect);
for i = 1:length(uniqueCasts)
currentCast = uniqueCasts(i); % Get the current cast
% Filter data for the current cast
filteredData = NutrientDataPlots(NutrientDataPlots.Cast == currentCast, :);
% Create a new figure for each cast
figure;
% Define the number of subplots
numPlots = 4; % Change this to the number of plots you want
% Create a horizontal tiled layout
tiledlayout(1, numPlots); % 1 row and numPlots columns
for j = 1:numPlots
% Create a new tile for each subplot
%nexttile;
currentTransect = specificTransects(j);
transectData = NutrientDataPlots(NutrientDataPlots.Transect == currentTransect, :);
% Create subplot for each transect
subplot(1, numPlots, j);
hold on; % Hold on to plot multiple lines
plot(filteredData.PhosphateML002, filteredData.NominalDepthm, '-o', 'DisplayName', 'PO4'); % Plot phosphate
plot(filteredData.SilicateML05, filteredData.NominalDepthm, '-s', 'DisplayName', 'SiO2'); % Plot silicate
plot(filteredData.NitriteML002, filteredData.NominalDepthm, '-s', 'DisplayName', 'NO2'); % Plot Nitrite
plot(filteredData.NitrateML, filteredData.NominalDepthm, '-s', 'DisplayName', 'NO3'); % Plot Nitrate
plot(filteredData.AmmoniaML003, filteredData.NominalDepthm, '-s', 'DisplayName', 'NH3'); % Plot Ammonia
hold off;
% Set axis labels
xlabel('Nutrient Concentration (µmol/L)','FontSize', 10);
ylabel('Depth (m)','FontSize', 10);
% Add a legend
legend show;
% Invert y-axis to have depth increasing downwards and change axis location to the top
set(gca,'XAxisLocation','top','YAxisLocation','left','ydir','reverse');
grid on; % Add grid for better visibility
% Create multiline title with the current cast
multilineTitleWithData = {
sprintf('%s Nutrient QC', char(filteredData.StudyName(1))), ...
sprintf('Cast %d %s %s', currentCast, char(filteredData.Transect(1)), char(filteredData.Station(1))), ...
sprintf('%s %s', char(filteredData.FilterType(1)), char(filteredData.StationNote(1)))};
% Display the title
title(multilineTitleWithData, 'FontWeight', 'bold');
end
end
  1 个评论
dpb
dpb 2025-4-29
You forgot to attach the data file...
So, if the unique casts are what you want plotted side-by-side, then you'll neeed to create the layout by that variable and then plot whatever it is that is intended for those sequentially if going to increment the tile.
Otherwise, you would have to create the overall arrangement of the total number of tiles on each figure and populate each particular tile with the proper data selection...probably doable, but likely to get convoluted in practice.
Without the data itself to look at and coming in completely cold, it's pretty tough for folks to fully comprehend what is the data structure and what it is that is expected from it...starting by sharing the data would be first step.

请先登录,再进行评论。

采纳的回答

Ruchika Parag
Ruchika Parag 2025-6-5
Hi @SD, you're on the right track, but the issue was due to the loop structure. You were looping over casts and then trying to vary transects inside that loop, which caused it to repeat the same cast multiple times.
Fix: Instead, loop over each Transect (region) first, and then within that, loop over the Casts that belong to that transect. For each cast, filter the data and plot the nutrient profiles as subplots side by side.
Since you mentioned variable numbers of casts and transects, I used unique() to dynamically group them. I also removed the Excel dependency and generated dummy data for testing. Here's a working example:
clear; close all; clc;
% Create dummy nutrient data
numTransects = 3;
castsPerTransect = [2, 3, 2]; % Each transect can have different number of casts
depths = linspace(0, 100, 10)'; % Depth levels
% Nutrient names
nutrients = {'PO4', 'SiO2', 'NO2', 'NO3', 'NH3'};
% Generate dummy table
allData = table();
for t = 1:numTransects
for c = 1:castsPerTransect(t)
n = length(depths);
data = table;
data.StudyName = repmat("P2402", n, 1);
data.Transect = repmat("T" + string(t), n, 1);
data.Cast = repmat(c, n, 1);
data.Station = repmat("Station" + string(c), n, 1);
data.NominalDepthm = depths;
data.PhosphateML002 = rand(n,1)*5 + 0.1;
data.SilicateML05 = rand(n,1)*10 + 1;
data.NitriteML002 = rand(n,1)*2;
data.NitrateML = rand(n,1)*10;
data.AmmoniaML003 = rand(n,1)*1.5;
allData = [allData; data];
end
end
% Plotting
transects = unique(allData.Transect);
for t = 1:length(transects)
transectData = allData(allData.Transect == transects(t), :);
casts = unique(transectData.Cast);
numCasts = length(casts);
figure('Name', sprintf('Transect %s', transects(t)), 'Position', [100, 100, 300*numCasts, 600]);
tiledlayout(1, numCasts);
for c = 1:numCasts
castData = transectData(transectData.Cast == casts(c), :);
nexttile;
hold on;
plot(castData.PhosphateML002, castData.NominalDepthm, '-o', 'DisplayName', 'PO4');
plot(castData.SilicateML05, castData.NominalDepthm, '-s', 'DisplayName', 'SiO2');
plot(castData.NitriteML002, castData.NominalDepthm, '-^', 'DisplayName', 'NO2');
plot(castData.NitrateML, castData.NominalDepthm, '-d', 'DisplayName', 'NO3');
plot(castData.AmmoniaML003, castData.NominalDepthm, '-x', 'DisplayName', 'NH3');
hold off;
xlabel('Nutrient Conc. (µmol/L)');
ylabel('Depth (m)');
title(sprintf('Cast %d', casts(c)));
set(gca, 'YDir', 'reverse', 'XAxisLocation', 'top');
legend show;
grid on;
end
sgtitle(sprintf('Nutrient Profile - Transect %s', transects(t)), 'FontWeight', 'bold');
end
This creates one figure per transect and shows each cast’s depth profile side by side. Hope it helps!
  1 个评论
SD
SD 2025-6-7
This totally helped!!! I was getting confused a bit with the looping. I appreciate your primary title structure as well. This was next on my list of things to do. I was able to keep mutliple title lines for each individual plot. Looks a lot nicer. I still have a few errors since I have more then 10 plots during some transects but I believe if I stack the plots where 2 am is on top and noon is down below the error should go away. This would also give me a nice structure for the plots since the 2am casts were shallower. Thank you for more then just an assist. Your are the best and enjoy your weekend.!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by