MATLAB: Specify a fixed color for zero in imagesc [duplicate] - matlab

I have a wide range of values and while plotting as a scatter(x,y,z), the colorbar showing the z axis shows a wide range of values, now I am not interested in the lower range values. Is there any method to change the range in color bar.
I have the following part of my code to plot, I also intend to plot the log plot. For eg. I want to set the range in my log plot to 14 to the maximum value.
I want some values not to be displayed at all. so that the color bar has a limited range, say from 14 to maximum. At present it is showing from 9 to maximum in the log plot.
scatter(x(1:end-1), y(1:end-1), 5, gnd);
title('G plot (m^-^2)');
colorbar('eastoutside');
xlabel(' X-axis (microns)');
ylabel('Y-axis (microns)');
figure;
log_g=log10(gnd);
scatter(x(1:end-1), y(1:end-1), 5,log_g);
colorbar('eastoutside');
xlabel(' X-axis (microns)');
ylabel('Y-axis (microns)');
title('G Density, log plot (m^-^2)');

I believe that caxis is the command you're looking for. Usage:
caxis([minValue maxValue])
Using caxis like this, all values outside the range [minValue maxValue] will be coloured with the lowest or highest value in the colormap, respectively.
Since colorbar and friends use colormap, you'll have to adjust the current colormap if you want to adjust the number of colors used. Do this like so:
%# get current colormap
map = colormap;
%# adjust for number of colors you want
rows = uint16(linspace(1, size(map,1), NUM_COLORS)) ;
map = map(rows, :);
%# and apply the new colormap
colormap(map);
Of course, combining this with caxis is the most powerful.
If you don't want to show some values outside of range, that's not a job for colorbar or caxis, that's up to you -- you'll have to adjust the data that's plotted so that all values you don't want plotted are NaN. Doing so will make Matlab understand that you don't want to plot these data:
data( indices_to_data_not_to_plot ) = NaN;
surf(x,y,data); %# or whatever you're using

How about this?
% don’t know why, but apparently your x and y are one value too long?
x = x(1:end-1); y = y(1:end-1);
% only plot values of 14 or higher
scatter(x(gnd>=14), y(gnd>=14), 5, gnd(gnd>=14);

Try this:
cmap = colormap; % get current colormap
cmap=cmap([min max],:); % set your range here
colormap(cmap); % apply new colormap
colorbar();

Related

How to color multiple lines based on their value?

I produced a plot that contains 50 curves and each of them corresponds to a specific value of a parameter called "Jacobi constant", so I have 50 values of jacobi constant stored in array called jacobi_cst_L1:
3.000900891023230
3.000894276927840
3.000887643313580
3.000881028967010
3.000874419173230
3.000867791975870
3.000861196034850
3.000854592397690
3.000847948043080
3.000841330136040
3.000834723697250
3.000828099771820
3.000821489088600
3.000814922863360
3.000808265737810
3.000801695858850
3.000795067776960
3.000788475204760
3.000781845363950
3.000775192199620
3.000768609354090
3.000761928862980
3.000755335851910
3.000748750854930
3.000742084743060
3.000735532899990
3.000728906460450
3.000722309400740
3.000715644446600
3.000709016645110
3.000702431180730
3.000695791284050
3.000689196186970
3.000682547292110
3.000675958537960
3.000669315388860
3.000662738391370
3.000656116141060
3.000649560630930
3.000642857256680
3.000636330415510
3.000629657944820
3.000623060310100
3.000616425935580
3.000609870077710
3.000603171772120
3.000596554947660
3.000590018845460
3.000583342259840
3.000576748353570
I want to use a colormap to color my curves and then show in a lateral bar the legend that show the numerical values corresponding to each color of orbit.
By considering my example image, I would want to add the array of constants in the lateral bar and then to color each curve according the lateral bar.
% Family of 50 planar Lyapunov orbits around L1 in dimensionless unit
fig = figure;
for k1 = 1:(numel(files_L1_L2_Ly_prop)-2)
plot([Ly_orb_filt(1).prop(k1).orbits.x],[Ly_orb_filt(1).prop(k1).orbits.y],...
"Color",my_green*1.1); hold on %"Color",my_green*1.1
colorbar()
end
axis equal
% Plot L1 point
plot(Ly_orb_filt_sys_data(1).x,Ly_orb_filt_sys_data(1).y,'.',...
'color',[0,0,0],'MarkerFaceColor',my_green,'MarkerSize',10);
text(Ly_orb_filt_sys_data(1).x-0.00015,Ly_orb_filt_sys_data(1).y-0.0008,'L_{1}');
%Primary bodies plots
plot(AstroData.mu_SEM_sys -1,0,'.',...
'color',my_blue,'MarkerFaceColor',my_blue,'MarkerSize',20);
text(AstroData.mu_SEM_sys-1,0-0.001,'$Earth + Moon$','Interpreter',"latex");
grid on;
xlabel('$x$','interpreter','latex','fontsize',12);
ylabel('$y$','interpreter','latex','FontSize',12);
How can I color each line based on its Jacobi constant value?
You can use any colour map to produce a series of RGB-triplets for the plotting routines to read (Or create an m-by-3 matrix with elements between 0 and 1 yourself):
n = 10; % Plot 10 lines
x = 1:15;
colour_map = jet(n); % Get colours. parula, hsv, hot etc.
figure;
hold on
for ii = 1:n
% Plot each line individually
plot(x, x+ii, 'Color', colour_map(ii, :))
end
colorbar % Show the colour bar.
Which on R2007b produces:
Note that indexing into a colour map will produce linearly spaced colours, thus you'll need to either interpolate or calculate a lot to get the specific ones you need. Then you can (need to?) modify the resulting colour bar's labels by hand to reflect your input values. I'd simply use parula(50), treat its indices as linspace(jacobi(1), jacobi(end), 50) and then my_colour = interp1(linspace(jacobi(1), jacobi(end), 50), parula(50), jacobi).
So in your code, rather than using "Color",my_green*1.1 for each line, use "Color",my_colour(kl,:), where my_colour is whatever series of RGB triplets you have defined.

Setting axis limit for plot

I want to set the limit for X axis in this plot from 0 to 325. When i am using xlim to set the limits (commented in the code). It doesn't work properly. When i use xlim, the entire structure of plot changes. Any help will be appreciated.
figure
imagesc(transpose(all_area_for_visual));
colormap("jet")
colorbar('Ticks',0:3,'TickLabels',{'Home ','Field','Bad house','Good house'})
xlabel('Time (min)')
tickLocs = round(linspace(1,length(final_plot_mat_missing_part(2:end,1)),8));
timeVector = final_plot_mat_missing_part(2:end,1);
timeForTicks = (timeVector(tickLocs))./60;
xticks(tickLocs);
xticklabels(timeForTicks);
%xlim([0 325]);
ylabel('Car identity')
yticks(1:length(Ucolumnnames_fpm))
yticklabels([Ucolumnnames_fpm(1,:)])
If I get you right, you want to plot only part of the data in all_area_for_visual, given by a condition on tickLocs. So you should first condition the data, and then plot it:
% generate the vector of x values:
tickLocs = round(linspace(1,length(final_plot_mat_missing_part(2:end,1)),8));
% create an index vector (of logicals) that marks the columns to plot from the data matix:
validX = tickLocs(tickLocs<=325);
% plot only the relevant part of the data:
imagesc(transpose(all_area_for_visual(:,validX)));
% generate the correct ticks for the data that was plotted:
timeVector = final_plot_mat_missing_part(2:end,1);
timeForTicks = (timeVector(tickLocs(validX)))./60;
xticks(tickLocs(validX));
% here you continue with setting the labels, colormap and so on...
imagesc puts the data in little rectangles centered around integers 1:width and 1:height by default. You can specify what the x and y locations of each data point by adding two vectors to the call:
imagesc(x,y,transpose(all_area_for_visual));
where x and y are vectors with the locations along the x and y axes you want to place the data.
Note that xlim and xticks don’t change the location of the data, only the region of the axis shown, and the location of tick marks along the axis. With xticklabels you can change what is shown at each tick mark, so you can use that to “fake” the data locations, but the xlim setting still applies to the actual locations, not to the labels assigned to the ticks.
I think it is easier to plot the data in the right locations to start with. Here is an example:
% Fake your data, I'm making a small matrix here for illustration purposes
all_area_for_visual = min(floor(cumsum(rand(20,5)/2)),3);
times = linspace(0,500,20); % These are the locations along the time axis for each matrix element
car_id_names = [4,5,8,15,18]; % These are the labels to put along the y-axis
car_ids = 1:numel(car_id_names); % These are the locations to use along the y-axis
% Replicate your plot
figure
imagesc(times,car_ids,transpose(all_area_for_visual));
% ^^^ ^^^ NOTE! specifying locations
colormap("jet")
colorbar('Ticks',0:3,'TickLabels',{'Home ','Field','Bad house','Good house'})
xlabel('Time (min)')
ylabel('Car identity')
set(gca,'YTick',car_ids,'YTickLabel',car_id_names) % Combine YTICK and YTICKLABEL calls
% Now you can specify your limit, in actual time units (min)
xlim([0 325]);

Use multiple colorbars in one figure [duplicate]

I am fairly new to Matlab and have a few questions.
I got two surfaces and a plane in the same figure.
I want to use different colormap and colorbar for b and another colormap and colorbar for c. s is fixed color so it's not a problem.
Let me try to explain what I am trying to achieve:
cmap1=colormap(topobathy) -->cmap1 is 64x3 double as expected
cmap2=colormap(redwhitegreen)
create cdata1 using cmap1 (this is the first part I cannot figure out, how to scale z data using cmap1, by default CData contains the z values)
b=surf(x,y,z,cdata1)
colorbar for b using z values
c=pcolor(x,y,(z-z0)) - I want to use cmap2 for this one.
colorbar for c using (z-z0) values
Here is what I have so far and the problems I encounter
b=surf(x,y,z);
colormap(topobathy);
cbar1=colorbar;
set(get(cbar1,'ylabel'),'String', 'Elevation (m)', 'Rotation', 90)
hold on;
s=surf(x,y,z1,'FaceColor',[0.278 0.788 0.788])
hold on;
change=z-z0;
c=pcolor(x,y,change)
set(c,'ZData',100 + 0*change); %plotting it above the other surfaces
colormap(redwhitegreen)`
at this point colormap is set to redwhitegreen for b, colorbar of b
I can't get the second colorbar with its own clim etc.
I used the freezeColors and cbfreeze explained in this link:
http://blogs.mathworks.com/pick/2009/07/24/using-multiple-colormaps-in-a-single-figure/
but one thing works while messing another thing (probably all my fault). I want to learn how to have complete control over my objects without using external m files anyway.
Any help is appreciated.
The basic idea is that you concatenate the colormaps, and then shift/scale the color data (CData) of the different plot handles to line up with the desired portions of the colormap. So, without knowing what your custom functions or specific data are, you could do something like colormap(topobathy(64); redwhitegreen(64)) and then scale the CData of b into the range [1,64] and the CData of c into the range [65,128].
There is an excellent guide on the MathWorks website that explains all this (even uses surf() and pcolor() like your example):
http://www.mathworks.com/support/tech-notes/1200/1215.html#Example_1
For the colorbar, you can just fake out the ticks and labels in a similar manner. Here is rough shot at making a color bar for the above example:
h = colorbar;
ticks = [1 16:16:64 64:16:128];
ticks(5:6) = [62 66];
set(h, 'YTick', ticks);
labels = num2str(repmat(linspace(min(Z(:)), max(Z(:)), 5), 1, 2)', 2);
set(h, 'YTickLabel', labels)
I had the same problem as you, and the best (and only) solution I found was the next:
Concatenate both colormaps I wanted:
cmap1 = jet(64); cmap2 = copper(64); color_map = [cmap1 ; cmap2];
So, the first colormap (cmap1) will be used for Axes1, and the second colormap (cmap2) for Axes2. I guess that Axes1 and Axes2 are in the same figure.
Normalize data, giving a scale from 0 to 1 for data of Axes1, and from 1 to 2 for data of Axes2. So, Axes1 in [0, 1] and Axes2 in [1 2].
data1 = data1 - lower_limit1;
data1 = double(data1./(upper_limit1 - lower_limit1));
For data set of Axes 2:
data2 = data2 - lower_limit;
data2 = double(data2./(upper_limit2 - lower_limit2)) + 1;
When representing them:
Axes1:
pcolor(handle_axes1, x_axis, y_axis, data1); shading(handle_axes1,'FLAT');
caxis(handle_axes1, [0 2]);
% Colorbar
h_colorbar = colorbar('peer', handle_axes1);
set(h_colorbar, 'YLim', [0 1]);
labels = num2str(linspace(lower_limit1, upper_limit1, 6)', 2);
set(h_colorbar, 'YTick', linspace(0, 1, 6));
set(h_colorbar, 'YTickLabel', labels);
Axes2:
pcolor(handle_axes2, x_axis, y_axis, data2); shading(handle_axes2,'FLAT');
caxis(handle_axes2, [0 2]);
% Colorbar
h_colorbar2 = colorbar('peer', handle_axes2);
set(h_colorbar2, 'YLim', [1 2]);
labels = num2str(linspace(lower_limit2, upper_limit2, 6)', 2);
set(h_colorbar2, 'YTick', linspace(1, 2, 6));
set(h_colorbar2, 'YTickLabel', labels);
Use pcolor or surf, depending on your needs.
Hope it helps!
Matlab has provided code for a function newclim which solves this problem cleanly by concatenating the colormaps into one colormap. I could only find this documentation on the 2012b Help, but not online.
Note that the axes used to update the CLim as the last step could be axes to surf plots, which is how I applied this code.
Calculating Color Limits
The key to this example is calculating values for CLim that cause each surface to use the section of the colormap containing the appropriate colors.
To calculate the new values for CLim, you need to know
The total length of the colormap (CmLength)
The beginning colormap slot to use for each axes (BeginSlot)
The ending colormap slot to use for each axes (EndSlot)
The minimum and maximum CData values of the graphic objects contained
in the axes. That is, the values of the axes CLim property determined
by MATLAB when CLimMode is auto (CDmin and CDmax).
First, define subplot regions and plot the surfaces.
im1 = load('cape.mat');
im2 = load('flujet.mat');
ax1 = subplot(1,2,1);
imagesc(im1.X)
axis(ax1,'image')
ax2 = subplot(1,2,2);
imagesc(im2.X)
axis(ax2,'image')
Concatenate two colormaps and install the new colormap.
colormap([im1.map;im2.map])
Obtain the data you need to calculate new values for CLim.
CmLength = length(colormap); % Colormap length
BeginSlot1 = 1; % Beginning slot
EndSlot1 = length(im1.map); % Ending slot
BeginSlot2 = EndSlot1 + 1;
EndSlot2 = CmLength;
CLim1 = get(ax1,'CLim'); % CLim values for each axis
CLim2 = get(ax2,'CLim');
Defining a Function to Calculate CLim Values
Computing new values for CLim involves determining the portion of the colormap you want each axes to use relative to the total colormap size and scaling its Clim range accordingly. You can define a MATLAB function to do this.
function CLim = newclim(BeginSlot,EndSlot,CDmin,CDmax,CmLength)
% Convert slot number and range
% to percent of colormap
PBeginSlot = (BeginSlot - 1) / (CmLength - 1);
PEndSlot = (EndSlot - 1) / (CmLength - 1);
PCmRange = PEndSlot - PBeginSlot;
% Determine range and min and max
% of new CLim values
DataRange = CDmax - CDmin;
ClimRange = DataRange / PCmRange;
NewCmin = CDmin - (PBeginSlot * ClimRange);
NewCmax = CDmax + (1 - PEndSlot) * ClimRange;
CLim = [NewCmin,NewCmax];
end
The input arguments are identified in the bulleted list above. The function first computes the percentage of the total colormap you want to use for a particular axes (PCmRange) and then computes the CLim range required to use that portion of the colormap given the CData range in the axes. Finally, it determines the minimum and maximum values required for the calculated CLim range and returns these values. These values are the color limits for the given axes.
Using the Function
Use the newclim function to set the CLim values of each axes. The statement
set(ax1,'CLim',newclim(BeginSlot1,EndSlot1,CLim1(1),...
CLim1(2),CmLength))
sets the CLim values for the first axes so the surface uses color slots 65 to 120. The lit surface uses the lower 64 slots. You need to reset its CLim values as well.
set(ax2,'CLim',newclim(BeginSlot2,EndSlot2,CLim2(1),...
CLim2(2),CmLength))
How the Function Works
MATLAB enables you to specify any values for the axes CLim property, even if these values do not correspond to the CData of the graphics objects displayed in the axes. The minimum CLim value is always mapped to the first color in the colormap and the maximum CLim value is always mapped to the last color in the colormap, whether or not there are really any CData values corresponding to these colors. Therefore, if you specify values for CLim that extend beyond the object's actual CData minimum or maximum, MATLAB colors the object with only a subset of the colormap.
The newclim function computes values for CLim that map the graphics object's actual CData values to the beginning and ending colormap slots that you specify. It does this by defining a "virtual" graphics object having the computed CLim values.

how to assign colors to positive and negative values

How can I plot a map area filled with colors and colors representing the values which are negative and positive. Colormap can only give colors to values in the range [0,1]. I want to increase the range.
Let x be the matrix with the values you want to visualize. Then you can use imagesc for a visualization:
x = (rand(100,100)-0.5)*10; % random values between -5 and 5
imagesc(x);
colorbar
Note that the colorbar is automatically adjusted to the range of date in x.
The simple way would be to do this:
Suppose you want to plot x
xpos=x;
xneg=x;
xpos(xpos<0)=NaN;
xneg(xneg>=0) = NaN;
plot(xpos)
hold all;
plot(xneg)

Matlab: How to assign different colormaps/colorbars to different surfaces in the same Figure

I am fairly new to Matlab and have a few questions.
I got two surfaces and a plane in the same figure.
I want to use different colormap and colorbar for b and another colormap and colorbar for c. s is fixed color so it's not a problem.
Let me try to explain what I am trying to achieve:
cmap1=colormap(topobathy) -->cmap1 is 64x3 double as expected
cmap2=colormap(redwhitegreen)
create cdata1 using cmap1 (this is the first part I cannot figure out, how to scale z data using cmap1, by default CData contains the z values)
b=surf(x,y,z,cdata1)
colorbar for b using z values
c=pcolor(x,y,(z-z0)) - I want to use cmap2 for this one.
colorbar for c using (z-z0) values
Here is what I have so far and the problems I encounter
b=surf(x,y,z);
colormap(topobathy);
cbar1=colorbar;
set(get(cbar1,'ylabel'),'String', 'Elevation (m)', 'Rotation', 90)
hold on;
s=surf(x,y,z1,'FaceColor',[0.278 0.788 0.788])
hold on;
change=z-z0;
c=pcolor(x,y,change)
set(c,'ZData',100 + 0*change); %plotting it above the other surfaces
colormap(redwhitegreen)`
at this point colormap is set to redwhitegreen for b, colorbar of b
I can't get the second colorbar with its own clim etc.
I used the freezeColors and cbfreeze explained in this link:
http://blogs.mathworks.com/pick/2009/07/24/using-multiple-colormaps-in-a-single-figure/
but one thing works while messing another thing (probably all my fault). I want to learn how to have complete control over my objects without using external m files anyway.
Any help is appreciated.
The basic idea is that you concatenate the colormaps, and then shift/scale the color data (CData) of the different plot handles to line up with the desired portions of the colormap. So, without knowing what your custom functions or specific data are, you could do something like colormap(topobathy(64); redwhitegreen(64)) and then scale the CData of b into the range [1,64] and the CData of c into the range [65,128].
There is an excellent guide on the MathWorks website that explains all this (even uses surf() and pcolor() like your example):
http://www.mathworks.com/support/tech-notes/1200/1215.html#Example_1
For the colorbar, you can just fake out the ticks and labels in a similar manner. Here is rough shot at making a color bar for the above example:
h = colorbar;
ticks = [1 16:16:64 64:16:128];
ticks(5:6) = [62 66];
set(h, 'YTick', ticks);
labels = num2str(repmat(linspace(min(Z(:)), max(Z(:)), 5), 1, 2)', 2);
set(h, 'YTickLabel', labels)
I had the same problem as you, and the best (and only) solution I found was the next:
Concatenate both colormaps I wanted:
cmap1 = jet(64); cmap2 = copper(64); color_map = [cmap1 ; cmap2];
So, the first colormap (cmap1) will be used for Axes1, and the second colormap (cmap2) for Axes2. I guess that Axes1 and Axes2 are in the same figure.
Normalize data, giving a scale from 0 to 1 for data of Axes1, and from 1 to 2 for data of Axes2. So, Axes1 in [0, 1] and Axes2 in [1 2].
data1 = data1 - lower_limit1;
data1 = double(data1./(upper_limit1 - lower_limit1));
For data set of Axes 2:
data2 = data2 - lower_limit;
data2 = double(data2./(upper_limit2 - lower_limit2)) + 1;
When representing them:
Axes1:
pcolor(handle_axes1, x_axis, y_axis, data1); shading(handle_axes1,'FLAT');
caxis(handle_axes1, [0 2]);
% Colorbar
h_colorbar = colorbar('peer', handle_axes1);
set(h_colorbar, 'YLim', [0 1]);
labels = num2str(linspace(lower_limit1, upper_limit1, 6)', 2);
set(h_colorbar, 'YTick', linspace(0, 1, 6));
set(h_colorbar, 'YTickLabel', labels);
Axes2:
pcolor(handle_axes2, x_axis, y_axis, data2); shading(handle_axes2,'FLAT');
caxis(handle_axes2, [0 2]);
% Colorbar
h_colorbar2 = colorbar('peer', handle_axes2);
set(h_colorbar2, 'YLim', [1 2]);
labels = num2str(linspace(lower_limit2, upper_limit2, 6)', 2);
set(h_colorbar2, 'YTick', linspace(1, 2, 6));
set(h_colorbar2, 'YTickLabel', labels);
Use pcolor or surf, depending on your needs.
Hope it helps!
Matlab has provided code for a function newclim which solves this problem cleanly by concatenating the colormaps into one colormap. I could only find this documentation on the 2012b Help, but not online.
Note that the axes used to update the CLim as the last step could be axes to surf plots, which is how I applied this code.
Calculating Color Limits
The key to this example is calculating values for CLim that cause each surface to use the section of the colormap containing the appropriate colors.
To calculate the new values for CLim, you need to know
The total length of the colormap (CmLength)
The beginning colormap slot to use for each axes (BeginSlot)
The ending colormap slot to use for each axes (EndSlot)
The minimum and maximum CData values of the graphic objects contained
in the axes. That is, the values of the axes CLim property determined
by MATLAB when CLimMode is auto (CDmin and CDmax).
First, define subplot regions and plot the surfaces.
im1 = load('cape.mat');
im2 = load('flujet.mat');
ax1 = subplot(1,2,1);
imagesc(im1.X)
axis(ax1,'image')
ax2 = subplot(1,2,2);
imagesc(im2.X)
axis(ax2,'image')
Concatenate two colormaps and install the new colormap.
colormap([im1.map;im2.map])
Obtain the data you need to calculate new values for CLim.
CmLength = length(colormap); % Colormap length
BeginSlot1 = 1; % Beginning slot
EndSlot1 = length(im1.map); % Ending slot
BeginSlot2 = EndSlot1 + 1;
EndSlot2 = CmLength;
CLim1 = get(ax1,'CLim'); % CLim values for each axis
CLim2 = get(ax2,'CLim');
Defining a Function to Calculate CLim Values
Computing new values for CLim involves determining the portion of the colormap you want each axes to use relative to the total colormap size and scaling its Clim range accordingly. You can define a MATLAB function to do this.
function CLim = newclim(BeginSlot,EndSlot,CDmin,CDmax,CmLength)
% Convert slot number and range
% to percent of colormap
PBeginSlot = (BeginSlot - 1) / (CmLength - 1);
PEndSlot = (EndSlot - 1) / (CmLength - 1);
PCmRange = PEndSlot - PBeginSlot;
% Determine range and min and max
% of new CLim values
DataRange = CDmax - CDmin;
ClimRange = DataRange / PCmRange;
NewCmin = CDmin - (PBeginSlot * ClimRange);
NewCmax = CDmax + (1 - PEndSlot) * ClimRange;
CLim = [NewCmin,NewCmax];
end
The input arguments are identified in the bulleted list above. The function first computes the percentage of the total colormap you want to use for a particular axes (PCmRange) and then computes the CLim range required to use that portion of the colormap given the CData range in the axes. Finally, it determines the minimum and maximum values required for the calculated CLim range and returns these values. These values are the color limits for the given axes.
Using the Function
Use the newclim function to set the CLim values of each axes. The statement
set(ax1,'CLim',newclim(BeginSlot1,EndSlot1,CLim1(1),...
CLim1(2),CmLength))
sets the CLim values for the first axes so the surface uses color slots 65 to 120. The lit surface uses the lower 64 slots. You need to reset its CLim values as well.
set(ax2,'CLim',newclim(BeginSlot2,EndSlot2,CLim2(1),...
CLim2(2),CmLength))
How the Function Works
MATLAB enables you to specify any values for the axes CLim property, even if these values do not correspond to the CData of the graphics objects displayed in the axes. The minimum CLim value is always mapped to the first color in the colormap and the maximum CLim value is always mapped to the last color in the colormap, whether or not there are really any CData values corresponding to these colors. Therefore, if you specify values for CLim that extend beyond the object's actual CData minimum or maximum, MATLAB colors the object with only a subset of the colormap.
The newclim function computes values for CLim that map the graphics object's actual CData values to the beginning and ending colormap slots that you specify. It does this by defining a "virtual" graphics object having the computed CLim values.