Clear Filters
Clear Filters

How to set the Z axis length and values constant in a 3D surface plot?

35 views (last 30 days)
I am using a 3D surface plot on 100 matrices (each matrix is 44 by 44). For every matrix, the maximum and minimum value in the Z direction is different and hence when I use the surface plot function, the length of Z axis keeps changing. But I want the length of Z axis and the values on it to remain constant. How to achieve that?

Answers (1)

Image Analyst
Image Analyst on 6 May 2016
Use zlim():
zlim([yourMinZValue, yourMaxZValue]);
  3 Comments
Image Analyst
Image Analyst on 6 May 2016
I didn't say use the max value of your data - I said to use the max and min values that you want it to use for all surface plots. So use
zlim([20, 100]);
if the lowest you think any of the 100 plots will go is 20 and the highest you think any of them will ever reach is 100. Call zlim() after after you plot to any axes control regardless of how many axes controls there are.
Javier García Romero
Javier García Romero on 29 Dec 2020
Edited: Javier García Romero on 29 Dec 2020
I have the same problem as Jasnoor Singh (I'm plotting a damped 3D sine wave) and the solution you gave doesn't work for me. This is what I coded, and the zlim I get in the plot are approx [-2, 3.5] instead of [-5, 5]:
clc
clear
x=-4*pi:0.1:4*pi;
y=-4*pi:0.1:4*pi;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2);
Z=5*exp(-R/5).*sin(R);
s = surf(X,Y,Z,'FaceColor','r','FaceAlpha','0.3','edgecolor','k');
zlim([-5 5]);
xlabel('x')
ylabel('y')
zlabel('z')
axis equal tight
EDIT: I've sorted it out. What is working for me is writing "axis([xmin xmax ymin ymax zmin zmax])" at the end instead of using zlim, so now it looks like this:
clc
clear
x=-4*pi:0.1:4*pi;
y=-4*pi:0.1:4*pi;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2);
Z=5*exp(-R/5).*sin(R);
s = surf(X,Y,Z,'FaceColor','r','FaceAlpha','0.3','edgecolor','k');
xlabel('x')
ylabel('y')
zlabel('z')
axis([-4*pi 4*pi -4*pi 4*pi -5 5])

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!