Plotting two surface plots in the same figure using two different colormaps
4 commentaires
Hi @Ira,
First, I will generate data for the two surface plots, I will use functions like peaks , if you want yiu can define your own data matrices.
[X, Y, Z1] = peaks(30); % Data for the first surface plot
Z2 = peaks(30) - 5; % Data for the second surface plot
Then I will plot the first surface plot using the 'turbo' colormap per your description.
figure;
surf(X, Y, Z1, 'FaceColor', 'interp', 'EdgeColor', 'none');
colormap turbo;
colorbar;
Then, plot the second surface plot using the 'gray' colormap and make it semitransparent by adjusting the alpha value, again per your description.
hold on;
surf(X, Y, Z2, 'FaceColor', 'interp', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
colormap gray;
Finally, you can further customize the plot by adding labels, titles, adjusting the view, or any other modifications as needed.
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Combined Surface Plots');
view(3); % Adjust the view angle if needed
Please see attached plot. Hope, this answers your question.

Hi @Ira,
The updated code snippet demonstrates adjusting the colormaps and overlaying the axes,and efficiently presents two distinct surfaces with varying color representations. Here is updated code along with attached plot.
% Define the colormap for the second axis
colormap(gray);
% Generate meshgrid data
[X, Y] = meshgrid(-2:0.1:2);
% Sample data for surface plot 1
Z1 = X.^2 - Y.^2;
% Sample data for surface plot 2
Z2 = sin(X) + cos(Y);
% Create a figure
figure;
% Create subplots for each surface plot
ax1 = subplot(1,2,1); % Axes for surface plot 1
ax2 = subplot(1,2,2); % Axes for surface plot 2
% Plot the first surface on the first axis
surf(ax1, X, Y, Z1, 'FaceColor', 'interp', 'EdgeColor', 'none');
colormap(ax1, turbo);
% Plot the second surface on the second axis
surf(ax2, X, Y, Z2, 'FaceColor', 'interp', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
% Adjust the colormap for the second axis
colormap(ax2, gray);
% Overlay the second axis on top of the first axis
set(ax2, 'Position', get(ax1, 'Position'));
% Hide the second axis
axis(ax2, 'off');

Replacing Z1 & Z2 data with the following data below will display the attached plot below.
Z1 = peaks(41)-5; % Data for the first surface plot with correct dimensions
Z2 = peaks(41)-5; % Data for the second surface plot with correct dimensions

Réponses (2)


4 commentaires
0 votes
- Use seperate plotting axes. Each axes can have its own colormap. Make sure you pass the axes handle into the colormap() calls. You can ax2 = axes('Position', get(ax1, 'Position')) to place the second axes exactly where the first axes is, and then be careful to pass ax1 or ax2 as appropriate to each graphics operation.
- Alternately, plot normally with one colormap, and then call https://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors-unfreezecolors to convert the graphics object from indexed to RGB. Once the object is converted to RGB, colormap stops affecting it, so you can then go ahead and colormap the second palette.
2 commentaires
Catégories
En savoir plus sur Color and Styling dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!