Help with ocean transect plot

25 views (last 30 days)
Hi Matlabbers,
I'm fairly new to the community and struggling to make a transect plot of some nitrate data that I have from CTD casts. I have 11 stations across an area, each with different depths, and each with various nitrate concentration data.
So, what I want to produce is a transect plot, showing water depth on the y axis, distance between stations on x-axis and the nitrate concentration at each station on the z-axis, but in a 2D plot.
My issue is, for each station, the depth varies, and my nitrate concentration data is quite scattered, hence I want to interpolate it so that for missing data (NaNs), it takes the concentration from neighbouring stations at the same depth and generates a value. When I do this using my data, Matlab interpolates across the rows which doesnt work since there are various depths across each row. I can't really make a uniform depth profile because it differs so much between each station. I've tried fillmissing and interp1 to get around the fact that i have so many NaN values, but the results are really blocky.
Any suggestions?
This is what I currently get:
And this is the sort of look I'm after
Thanks in advance :)

Accepted Answer

Ruchika Parag
Ruchika Parag on 26 Jun 2024
Hi Lisa,
Here are some suggestions to help you achieve your goal:
  1. Interpolate Data in 2D: Use griddata or scatteredInterpolant to interpolate your data in both depth and distance dimensions. This will help in filling NaN values more effectively.
  2. Create a Regular Grid: Generate a regular grid for both depth and distance to interpolate your scattered data onto this grid.
  3. Plotting: Use pcolor or contourf to plot the interpolated data.
Here is a code snippet explaining how you can do this:
% Define stations used
stn_use_SC = [5 8 9 14 15 16 19 20 21 22 23];
stn_name_SC = {'SC2' 'SC3' 'SC4' 'SC5' 'SC6' 'SC7' 'SC8' 'SC9' 'SC10' 'SC11' 'SC12'};
% Cumulative distance between each station
cumdistS = [0 2.6087 3.5718 4.7268 5.3693 5.9175 6.5523 7.1430 7.3480 7.8354 7.9372];
% Extract relevant data
depths = Depth(:, stn_use_SC);
nitrate = Nitrate(:, stn_use_SC);
% Flatten the data to create vectors for griddata
depths_vector = depths(:);
cumdistS_vector = repmat(cumdistS, size(depths, 1), 1);
cumdistS_vector = cumdistS_vector(:);
nitrate_vector = nitrate(:);
% Remove NaN values from the vectors
valid_indices = ~isnan(nitrate_vector);
depths_vector = depths_vector(valid_indices);
cumdistS_vector = cumdistS_vector(valid_indices);
nitrate_vector = nitrate_vector(valid_indices);
% Create a regular grid for interpolation
depth_grid = linspace(min(depths_vector), max(depths_vector), 100);
distance_grid = linspace(min(cumdistS_vector), max(cumdistS_vector), 100);
[distance_mesh, depth_mesh] = meshgrid(distance_grid, depth_grid);
% Interpolate nitrate data onto the regular grid
nitrate_interpolated = griddata(cumdistS_vector, depths_vector, nitrate_vector, distance_mesh, depth_mesh, 'linear');
% Plot the interpolated data
figure;
pcolor(distance_mesh, depth_mesh, nitrate_interpolated);
shading interp;
colorbar;
colormap('parula');
hold on;
% Plot the original data points
plot(cumdistS, depths, 'k.', 'MarkerSize', 10);
% Add contour lines
contour(distance_mesh, depth_mesh, nitrate_interpolated, 'k', 'ShowText', 'on');
% Adjust the position of station names to be above the plot area
y_label_position = -max(depth_grid) * 0.05; % Adjust this value as needed
for i = 1:length(stn_name_SC)
text(cumdistS(i), y_label_position, stn_name_SC{i}, 'fontsize', 5, 'rotation', 45, 'HorizontalAlignment', 'right');
end
% Label axes
xlabel('Distance (km)');
ylabel('Depth (m)');
% Reverse both axes
set(gca, 'XDir', 'reverse', 'YDir', 'reverse', 'fontsize', 12);
% Adjust y-axis limits
ylim([0 400]);
  1 Comment
Lisa Friberg
Lisa Friberg on 28 Jun 2024
Amazing, that worked perfectly! Thank you so so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!