Plotting 4-D graph, 3-D with 4th dimension colored

149 views (last 30 days)
Bus141
Bus141 on 5 May 2015
Commented: Walter Roberson on 23 Jan 2020
I am trying to plot a 3-D figure from 4 variables, the first 3 are semi-repetitive and the 4th one is the one from which I would like colors. In my actual set the 4th is not repetitive in any order but repetitive more randomly. Below is a generic code of my data looks like.
x=1:2:20; x=x'; x=[x;x;x;x;x;x];
y=ones(10,1); y=[y;y+1;y+2];y=[y;y];
z=ones(30,1)*2; z=[z;z+1];
c=1:4; c=c'; c=repmat(c,15,1);
scatter3(x,y,z,c,'filled')
As you can see from the figure it is hard to tell which dot is for which value of c. I am not set on using this graph if better ideas are out there. Any help is appreciated. Thanks!

Accepted Answer

Cindy Solomon
Cindy Solomon on 7 May 2015
Hi Bus,
What sort of data are you trying to visualize? Without a specific idea, a few general recommendations for you:
  • For better visualization of functions of 4 variables, I recommend taking a look at sliceomatic
  • For visualizing the fourth dimension specifically with color, you can change the size of the markers and/or colors of each point with scatter3. For example, if I replaced your last line with:
markerSize = 100;
scatter3(x,y,z,markerSize,c,'filled')
colorbar
then the plot will now have data points whose color is the linear mapping of the values in C to the colors in the current colormap- the way you had it before was altering the size instead of color. If you would prefer to specify a RBG color value instead, you could also give it a 3 column matrix with the number of rows in C equal to the length of X, Y, and Z. This way, each row could then specify an RGB color value for each marker. This is especially handy if you want to change each marker separately (by size and/or color). You could also change the RGB value to be proportional to the value of c (ex: c/max(c)) as well.
Hope this helps!
  1 Comment
Bus141
Bus141 on 7 May 2015
Thanks! Yeah it appears I was trying to place the values in the wrong place.
I am having an issue getting the legend to make sense. The fourth dimension I am using has discrete values, so while the colorbar helps if you know the graph really well, I think it would be easier to interpret if the values corresponded to a legend or key. Any thoughts?

Sign in to comment.

More Answers (1)

Cindy Solomon
Cindy Solomon on 7 May 2015
Hi Bus,
If your fourth dimension is only a small number of discrete values, I agree a legend would make more sense (Although you can make a colorbar with only 10 entries, a legend is probably a bit easier to read.)
You might have had some trouble creating different legend entries if you only had one plot object- it would be easiest to plot each c-value independently like you would when overlaying plots on top of each other with "hold on." For example, you could do something like:
cVals = unique(c); % Find all unique values of c
for i = 1:numel(cVals) % For every one of those unique values
indices = find(c == cVals(i)); % Find the corresponding indices
scatter3(x(indices),y(indices),z(indices),100,'filled') % Plot
hold on
end
legend('C = 1', 'C = 2','C = 3','C = 4');
to get the behavior that I think you are describing?
  4 Comments
Walter Roberson
Walter Roberson on 23 Jan 2020
Humans have a hard time distinguishing that many colors. Realistically you should only use perhaps 25 colors. You can combine them with different line styles and marker shapes.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!