Colormap direction in ribbon plot

2 views (last 30 days)
Merijn
Merijn on 8 Dec 2011
Edited: Voss on 12 Mar 2025
I'm trying to make a ribbon plot with a scaled CDatamapping colormap. How can I change the axis of the colormap? For example:
[x,y] = meshgrid(-3:.5:3,-3:.1:3);
z = peaks(x,y);
ribbon(y,z)
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap jet
creates a ribbon plot with colormapping in the x-direction. I would like to have the colormapping in the z-direction. Any ideas how to do this? Thank you

Answers (1)

Naga
Naga on 12 Mar 2025
To change the colormapping of a ribbon plot to be based on the z-direction instead of the x-direction, you can manipulate the 'CData' property of the surface objects created by the ribbon. By default, the ribbon plot uses x-values to determine the color mapping. By setting 'h(i).CData = z(:, i);', you use the z-values to determine the color of each ribbon. Ensure the 'CDataMapping' property is set to 'scaled' so that the colormap is scaled according to the range of z-values.
Here’s how you can achieve this:
[x, y] = meshgrid(-3:0.5:3, -3:0.1:3);
z = peaks(x, y);
h = ribbon(y, z);
% Set the colormap
colormap jet
% Adjust the CData property to use z-values for coloring
for i = 1:length(h)
h(i).CData = z(:, i);
end
set(h, 'CDataMapping', 'scaled');
xlabel('X')
ylabel('Y')
zlabel('Z')
% Add colorbar for reference
colorbar

Categories

Find more on Colormaps 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!