MATLAB: How to add fixed Colorbar based on a vector for 2D scatter plot with 3 vectors

colorbar

I want to compare two [2D scatter] plots that both have three variables (x,y,z). I'm plotting x against y and showing vector z for both plots by color. I need to make the colorbar fixed so I can compare the two plots. How can I make this colorbar range fixed for both? Thank you in advance
if true
figure
pointsize = 10;
scatter(x1, y1, pointsize, z1)
colorbar;
figure
pointsize = 10;
scatter(x2, y2, pointsize, z2)
colorbar;
end

Best Answer

One option is to normalize the ‘z’ values by scaling them with respect to the range of both ‘z1’ and ‘z2’:
figure
pointsize = 10;
scatter(x1, y1, pointsize, (z1-min([z1(:);z2(:)]))/max([z1(:);z2(:)]))
colorbar;
figure
pointsize = 10;
scatter(x2, y2, pointsize, (z2-min([z1(:);z2(:)]))/max([z1(:);z2(:)]))
colorbar;
The colorbar will be the same for both.