Disappear of z coordinate when plotting 3D

16 views (last 30 days)
KD39
KD39 on 10 Nov 2019
Answered: Image Analyst on 10 Nov 2019
Hi,
When I use figure and hold on with a for loop to plot a 3D plot, the z coordinate component always disappears which means the plot becomes 2D. Could anyone help the solve the issue? Thanks in advance. The folloing is the general code.
figure();
for index=1:max_val_1
other_code_here.....
for i=1:max_val_2
other_code_here.....
plot3(edge(i,1),edge(i,2),edge(i,3), 'o');
xlabel('x')
xlim([min max])
ylabel('y')
ylim([min max])
zlabel('z')
zlim([min max])
end
grid on
hold on
end

Answers (2)

the cyclist
the cyclist on 10 Nov 2019
I think you can "manually" fix this by setting the view. For example, try
view(15,15)
after the plot is made.
I've seen a couple questions like this lately, so I'm wondering if there is some kind of bug with MATLAB defaulting to the "2-D" view (i.e. directly from "above", along the z-axis) when it should not be. I haven't been able to pin it down exactly enough for a bug report, though.
  2 Comments
KD39
KD39 on 10 Nov 2019
Hi the cyclist,
Thanks for your reply. Also I just discovered that if I move the hold on right after the plot3 statement only the first 'index' loop is plotted. The rest of the for loop 'index' won't be plotted.
This might be MATLAB bug but not sure.
Regards
the cyclist
the cyclist on 10 Nov 2019
I think you probably want the hold on statement immediately after the figure command (assuming your goal is to show every plot in that figure window). See the documentation for hold for the details.

Sign in to comment.


Image Analyst
Image Analyst on 10 Nov 2019
Since you're plotting only a single point with each call to
plot3(edge(i,1),edge(i,2),edge(i,3), 'o');
you should place "hold on" inside that inner loop. And it should plot all max_val_2 points. Note that there is no reference to "index" inside your "i" loop so you should not expect anything different unless the badly-named "edge" vector changes within the outer "index" loop. By the way, edge(), min(), and max() are the names of a built-in functions so you don't want to use those as the names of your variable.
It's not a bug - it's a feature.bmp

Community Treasure Hunt

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

Start Hunting!