How could one create a contourslice plot with the color fill property of a contourf plot?

11 views (last 30 days)
As mentioned in the title, I am attempting to utilize the the function 'contourslice' in order to plot multiple 2-dimensional planes in series, representing data points on an X-Y grid at different depths, Z. I've successfully plotted two slices at different depths with 'contourslice' and I've also plotted one slice with 'contourf'. I find that it is easier to visual the data when the space between contour isolines is appropriately colored (as in 'contourf').
Is it possible to combine the filled isoline 'contourf' property with the multi-dimensional capability of 'contourslice'?

Answers (1)

Prathamesh
Prathamesh on 18 Feb 2025
I understand that you are using the function 'contourslice' to plot multiple 2-dimensional planes in series, with the space between contour isolines colored for better visualization and are unsure if this can be combined with the multi-dimensional capability.
Here is an example code which achieves the same using ‘slice’ and ‘contourf’ functions :
Code:
% Example data
[X, Y, Z] = meshgrid(1:10, 1:10, 1:5); % Create a 3D grid
V = rand(10, 10, 5); % Random volumetric data
% Define the Z-levels you want to slice
zLevels = [1, 3, 5];
figure;
hold on;
% Plot contour lines using contourslice
contourslice(X, Y, Z, V, [], [], zLevels);
% Overlay filled contour plots manually
for i = 1:length(zLevels)
zLevel = zLevels(i);
% Extract the slice data
V_slice = V(:, :, zLevel);
% Create a contourf plot for the current slice
[~, h] = contourf(X(:, :, zLevel), Y(:, :, zLevel), V_slice);
% Adjust the ZData to position the contour plot in 3D space
for j = 1:length(h)
h(j).ZData = zLevel * ones(size(h(j).XData));
end
end
% Set the view to 3D
view(3);
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;
grid on;
axis tight;
hold off;
title('Contour Slices with Filled Contours in 3D');
Output:

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!