This should be simple, but I'm having trouble getting the scaling of a custom color map to match the range of values that I'm plotting. I don't quite understand how to get the range on the colorbar to match the range of the Z-dimension data. Here's a simple example that I'm trying to get working:
maxval = 5;
num = 10;
X = linspace(0,maxval,num);
Y = linspace(0,maxval,num);
Z = linspace(0,maxval,num);
cMap = interp1([0;1],[1 .5 0; .5 1 0],linspace(0,1,num));
scatter3(X,Y,Z,50,cMap,'Filled');
view(-75,20);
colormap(cMap)
colorbar
Which gives this:
I want the top value in the colorbar to be 5 in this example. I've tried modifying the [0;1] in the interp1 function, but I can't seem to get the correct settings.
I'm using version 2017a, if that matters.
CodePudding user response:
The 5th input in scatter3
can represent a completely independent axis of data. If you want the color to match the Z axis data, then you need to pass it in twice.
The custom colormap (which can be a different size than the data) in interpolated to this 5th input to make the display.
Pragmatically, I think you want to make the following change to your code:
maxval = 5;
num = 10;
X = linspace(0,maxval,num);
Y = linspace(0,maxval,num);
Z = linspace(0,maxval,num);
cMap = interp1([0;1],[1 .5 0; .5 1 0],linspace(0,1,num));
scatter3(X,Y,Z,50,Z,'Filled'); % <-- note change here
view(-75,20);
colormap(cMap)
colorbar