How can I create a plot like this using MATLAB?

1 view (last 30 days)
Hey there!
I'm trying to create a plot like the one shown below:
I am particularly concerned about how to do two things:
  1. How can I make the sphere to be shaded with a colormap depending on the incident angle of the light?
  2. (Less important) How can I tilt the sphere, like the tilt the planet has?
Thank you in advance!

Accepted Answer

Star Strider
Star Strider on 27 Jan 2016
Rotating the colormap is difficult, and I haven’t solved that. Plotting, rotating, and lighting the sphere is relatively straightforward:
[Xs,Ys,Zs] = sphere(50);
Axis = [[0;0], [0;0], [-1;1]];
figure(1)
hs = surf(Xs, Ys, Zs);
hold on
ls = plot3(Axis(:,1), Axis(:,2), 2*Axis(:,3));
hold off
rotate(hs, [-2, 1, 0], 23.5)
rotate(ls, [-2, 1, 0], 23.5)
axis equal
shading interp
camlight(60,-10)
colormap(jet(50))
  3 Comments
Andres
Andres on 27 Jan 2016
Thank you both! I will be using Star Strider code to generate and rotate the sphere, and then I will use what Mike Garrity proposed for the light direction.
Also, in order to set the light intensity depending on the distance to the object, I found that I can use caxis. So, the code combined, just to share it in case someone else needs to do something similar:
% Define the size of the mesh for the sphere
meshsize = 20;
[Xs,Ys,Zs] = sphere(meshsize);
Axis = [[0;0], [0;0], [-1;1]];
% Replace this with "real" light direction
dirvec = [0 -1 0]; % Light coming from -y axis
dirvec = dirvec/norm(dirvec);
% Xs,Ys,Zs are normal vectors of
% sphere, so we can dot them with
% the light direction vector.
Cs = dirvec(1)*Xs ...
+ dirvec(2)*Ys ...
+ dirvec(3)*Zs;
% Then clamp
Cs = max(0,Cs);
% And use as 4th arg to surf
hs = surf(Xs, Ys, Zs, Cs);
hold on
axis equal
ls = plot3(Axis(:,1), Axis(:,2), 2*Axis(:,3));
% Set the colormap to jet
colormap(jet(meshsize));
% Set the distance of the object 1 is near, > 1 is far
caxis([-0.1, 1]);
Also, I'm gonna use the rotation that Star proposed to make the tilt of the planet, and then try to put all together.
Thank you so much! You are awesome!
Star Strider
Star Strider on 27 Jan 2016
Edited: Star Strider on 27 Jan 2016
Our pleasure!
Mike, thank you for your contribution. That would make a good blog post. (Hint!)
Andres, when you’re satisfied that your code does what you want and is reasonably robust, consider documenting it and posting it on the File Exchange. I’m certain others will find it useful. It’s also easier to search the File Exchange than it is to search Answers.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!