Show plot with different "sampling" number side-by-side
Mostra commenti meno recenti
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 Commento
dpb
il 29 Apr 2025
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.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Annotations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



