MATLAB Answers

0

Why don't my labels appear in a 3D plot?

Asked by Jasmine Alvarez on 1 Mar 2018
Latest activity Commented on by Jan
on 2 Mar 2018
Accepted Answer by Jan
I do not know why my axis labels and title label will not appear in this 3D plot. Matlab doesn't say there's any errors, but the labels just do not exist. I've tried not specifying position(and units), but they still do not appear.
close;
RPM = 25;
R = .05;
h0 = .15;
%define constants and change units
omega = RPM*2*pi/60; %[rad/s]
g = 9.807; %[m/s^2]
r = linspace(0,R); %[m]
phi = linspace(0,2*pi); %[rad]
%PLOT
[r,phi] = meshgrid(r,phi);
[X,Y] = meshgrid(linspace(-R,R,2),linspace(-R,R,2));
t = linspace(0,2*pi);
%equation of surface
z = h0 - omega^2/(2*g)*(R^2/2 - r.^2); %[m]
%h0 (origin) plane
plane = h0*ones(2,2);
plane_1 = h0*ones([1,100]);
%radius at h0
R_h0 = R/sqrt(2);
%original figure
figure('Name','Graphical Display with Original Measurements',...
'Units','normalized','Position',[.005,.4,.4,.5]);
title('Original Surface Height and Surface with Rotation');
xlabel('Radius');
ylabel('Radius');
zlabel('Height');
surf(X,Y,plane,'FaceAlpha',0.5,'FaceColor',[1,.3,1]); hold on
plot3(R_h0*cos(t),R_h0*sin(t),plane_1,'Color','black'); hold on
surf(r.*cos(phi),r.*sin(phi),z,'LineStyle','none'); hold off
colorbar;

  0 Comments

Sign in to comment.

1 Answer

Answer by Jan
on 1 Mar 2018
Edited by Jan
on 1 Mar 2018
 Accepted Answer

The surf command clears the properties of the axes object, which removes the labels also. So insert them after the hold on:
...
figure('Name','Graphical Display with Original Measurements',...
'Units','normalized','Position',[.005,.4,.4,.5]);
surf(X,Y,plane,'FaceAlpha',0.5,'FaceColor',[1,.3,1]); hold on
plot3(R_h0*cos(t),R_h0*sin(t),plane_1,'Color','black');
surf(r.*cos(phi),r.*sin(phi),z,'LineStyle','none');
colorbar;
title('Original Surface Height and Surface with Rotation');
xlabel('Radius');
ylabel('Radius');
zlabel('Height');
I prefer to create the axes explicitly:
FigH = figure('Name','Graphical Display with Original Measurements',...
'Units','normalized','Position',[.005,.4,.4,.5]);
AxesH = axes('Parent', FigH, ...
'NextPlot', 'add'); % Equivalent to: "hold on"
xlabel('Radius');
ylabel('Radius');
zlabel('Height');
title('Original Surface Height and Surface with Rotation');
surf(AxesH, X,Y,plane,'FaceAlpha',0.5,'FaceColor',[1,.3,1]);
plot3(AxesH, R_h0*cos(t),R_h0*sin(t),plane_1,'Color','black');
surf(AxesH, r.*cos(phi),r.*sin(phi),z,'LineStyle','none');
colorbar(AxesH);
view(AxesH, 3);
Now the Axes Property NextPlot = 'add' enables holding all objects, such that the labels and title are kept also. Specifying the axes' handle is safer than relying on the current object, e.g. if the user click on another axes during the drawing.

  2 Comments

Thanks for the explanation! Thank you for your code, too; it's helping me organize mine. It's been like a year since I've used Matlab, so I'm trying to remember the basics in addition to learning new things.
If I'm going to have 3 figures, I do the same method with this (create figure and axes to plot on)? Or do I have to close things?
@Jasmine: You can simply try it. You can create new figures, close the former ones, or remove only the axes and colorbar and re-use the existing figure. The last one might be some milliseconds faster, but runtime is not your problem here, I assume.

Sign in to comment.