MATLAB: How to plot two figures with the same color limits in MATLAB 7.0 (R14)

colorbarMATLABmultipleplots

I have multiple plots each with their own range of values. I would like to plot them with the same color limits with one unified colormap that covers the range of values across all plots.

Best Answer

To create a unified colormap that covers the range of values across multiple plots, use the MIN and MAX commands to find the full range of values for all images. Then use the CAXIS command to set the color axis scaling to be the same for all plots.
The following code is an example of how to do this for two subplots of a figure:
% Create a common meshgrid for two plots having different z values
[x,y]=meshgrid(-1:0.05:1, -1:0.05:1);
% Two different z-data plots for the same x and y data points
z1=sin(x.^2+y.^2);
z2=cos(x+y);
% Minimum and maximum values of the two plots
% This is useful in setting the limits of the colorbar
bottom = min(min(min(z1)),min(min(z2)));
top = max(max(max(z1)),max(max(z2)));
% Plotting the first plot
subplot(1,2,1)
h1=surf(x,y,z1);
shading interp;
% This sets the limits of the colorbar to manual for the first plot
caxis manual
caxis([bottom top]);
% Plotting the second plot
subplot(1,2,2)
h2=surf(x,y,z2);
shading interp;
% This sets the limits of the colorbar to manual for the second plot
caxis manual
caxis([bottom top]);
colorbar;