MATLAB Answers

How to overlay two scatter plots over each other. Each with separate colour scales.

25 views (last 30 days)
I have two seperate scatter plots, attached at the bottom you can see both scatter plots as images.
I need the coloured scatter points to be over the top of the black/white scatter plot. I only need the scale for the coloured data.
How can this be achieved as they both use different colour scales?
Below is the code for each seperate scatter plot:
%Black/White scatter
figure('Name', 'CMM Data')
scatter3(cmm_xdata,cmm_ydata,cmm_zdata,[],norm_deviation,'.')
title('CMM Data')
xlim([-radius-5 radius+5])
ylim([-radius-5 radius+5])
xlabel('x (mm)')
ylabel('y (mm)')
zlabel('z (mm)')
view(2)
colormap(flipud(gray))
daspect([1 1 1])
grid('off')
hold('on')
%Coloured scatter
figure('Name', 'Sq Data')
scatter3(cci_x,cci_y,cci_z+1,60,sqcolourvalue,'filled')
colormap(jet)
c = colorbar
title('Sq Values')
xlim([-radius-5 radius+5])
ylim([-radius-5 radius+5])
xlabel('x (mm)')
ylabel('y (mm)')
zlabel('z (mm)')
view(2)
daspect([1 1 1])
grid('off')
Thanks
Joe

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 16 Nov 2020
An axes can have only one Colormap, so you have three options:
  1. Hard-code RGB color values for one (or both) of your scatter objects.
  2. Overlap two axes and make one transparent.
  3. Concatenate your two colormaps and scale your data to use different regions of the colormap.
I think option 1 is your best bet. Because you are using the colorbar for the right plot and not the left, I suggest using hard-coded RGB values for the plot on the left.
You don't provide the data, so let me give you an example of how you could do this using random data.
cdata = rand(1,25);
cmap = gray;
colorind = round(rescale(cdata, 1, height(cmap)));
rgb = cmap(colorind, :);
s1 = scatter(cos(1:25)*5+5,sin(1:25)*5+5,100,rgb,'filled');
hold on
s2 = scatter(1:10,1:10,100,rand(1,10),'filled');
colorbar
  3 Comments
Benjamin Kraus
Benjamin Kraus on 16 Nov 2020
The height command was updated in MATLAB R2020b to accept matrices as well as tables. I suspect the issue you were having is because you are using an older release. You can replace height(cmap) with size(cmap,1) for equivalent behavior.
One thing to note: In R2019b the default size of colormaps changed from 64 to 256. If you update, your hard-coded value of 64 will cause the code to return a different picture when the default size changes.
You can update your code to work the same in both releases by doing one of these options:
  1. Passing 64 as input to the gray command to specify the size (instead of using the default).
  2. Using size(cmap,1) instead of hard-coding 64.
Option 1
cmap = gray(64);
colorind = round(rescale(cdata, 1, 64));
Option 2
cmap = gray;
colorind = round(rescale(cdata, 1, size(cmap,1)));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!