67 views (last 30 days)
Show older comments
Bus141 on 5 May 2015
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored
Commented: Walter Roberson on 23 Jan 2020
Accepted Answer: Cindy Solomon
Open in MATLAB Online
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!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Cindy Solomon on 7 May 2015
⋮
Open in MATLAB Online
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 Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Bus141 on 7 May 2015
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored#comment_283690
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 on 7 May 2015
Open in MATLAB Online
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 Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Bus141 on 7 May 2015
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored#comment_283712
Works perfectly. Thanks a lot, appreciate it!
Cindy Solomon on 8 May 2015
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored#comment_283853
No problem- happy to help! =)
Abhishek Chopra on 23 Jan 2020
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored#comment_789001
Hi Cindy,
My problem is almost the same, except I have 1000 unique values. Is there a way that I could assign 1000 unique colors or a way to visualzie them better? Thank you!
Walter Roberson on 23 Jan 2020
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/215166-plotting-4-d-graph-3-d-with-4th-dimension-colored#comment_789013
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.
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D Plots
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Tags
- 4-d plot
- scatter3
- 3-d color
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office