MATLAB: Does “contourf” show a different color than the one meant for this value in the colorbar

MATLAB

If I run the code below a value of 800 for example is shown as the color at the bottom of the colorbar even if it should be the color for 800.
maxval=100;
cmax=1000;
xmax=maxval;
ymax=maxval;
X=linspace(0,xmax,xmax+1);
Y=linspace(0,ymax,ymax+1);
XY = zeros(xmax+1,ymax+1);
for x = X
for y = Y
XY(x+1,y+1)=x*y;
end
end
figure(1);
contourf(X,Y,XY);
grid on;
caxis([ 0 cmax ]);
colorbar;

Best Answer

Your data consists of values between 0 and 10,000. "contourf" by default creates 9 linearly spaced contours. Hence the ranges are 0 to 1000, 1000 to 2000 and so forth. Since the value of 800 falls into the first area it is colored in the color belong to the lowest value in the range. In order to create contours for the range 0 to 1000, you can modify the "contourf" line of code as follows:
contourf(X,Y,XY,[0:100:1000]);