Require Surface Plot Color Bar Consistant across multiple plots.

8 vues (au cours des 30 derniers jours)
Jaye
Jaye le 31 Jan 2012
Réponse apportée : nick le 16 Avr 2025
I am plotting various different plots
figure
a=[-1 1 -1 -2; 1 2 3 4; 1 1 1 1; -2 -3 -4 -5; -4 -5 -6 -8]
surf([1 2 3 4],[1 2 3 4 5],a)
colorbar
figure
b=[-2 2 -2 -4; 2 4 6 8; 2 2 2 2; -4 -6 -8 -10; -8 -10 -8 -16]
surf([1 2 3 4],[1 2 3 4 5],b)
colorbar
And I want the colorbars to be consistant to enable easy comparison as to relative extreme values. I want the color representing -8 on one graph to be same color as -8 on the other graph. Is this possible?

Réponses (1)

nick
nick le 16 Avr 2025
Hello Jaye,
To achieve this, you need to set the color limits 'caxis' for both plots to be the same, as shown:
% Data for the first plot
a = [-1 1 -1 -2; 1 2 3 4; 1 1 1 1; -2 -3 -4 -5; -4 -5 -6 -8];
% Data for the second plot
b = [-2 2 -2 -4; 2 4 6 8; 2 2 2 2; -4 -6 -8 -10; -8 -10 -8 -16];
% Determine the global minimum and maximum values across both datasets
global_min = min(min([a(:); b(:)]));
global_max = max(max([a(:); b(:)]));
% Plot the first surface
figure;
surf([1 2 3 4], [1 2 3 4 5], a);
colorbar;
caxis([global_min global_max]); % Set consistent color limits
% Plot the second surface
figure;
surf([1 2 3 4], [1 2 3 4 5], b);
colorbar;
caxis([global_min global_max]); % Set consistent color limits
Kindly refer to the documentation by executing the following command in MATLAB Command Window to know more about 'caxis' function :
doc caxis

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by