
add lines through origin in 3D scatter plot
26 views (last 30 days)
Show older comments
hey everyone,
I am trying to use the xline, yline function to insert lines at x = 0 and y=0 in my score plot. This works. However, when for some reason the zline function does not work when I want to specify a colour.
xline(0,'--k');
yline(0,'--k');
zline(0,'--k');
But when I change zline to:
zline(0)
I get the z = 0 line in a yellow colour. Is there a way to make this line dashed and black?
0 Comments
Answers (2)
Adam Danz
on 9 Apr 2019
Edited: Adam Danz
on 15 Apr 2019
There is no zline() funciton built into matlab (2019a). Note that xline and yline became available in 2018b.
xline and yline plot the reference lines in the middle of the z axis. To plot the origin lines (or any other 3D reference lines),
xlim([-1 1]);
ylim([-1 1]);
zlim([-1 1]);
hold on;
line(2*xlim, [0,0], [0,0], 'LineWidth', 3, 'Color', 'k');
line([0,0], 2*ylim, [0,0], 'LineWidth', 3, 'Color', 'k');
line([0,0], [0,0], 2*zlim, 'LineWidth', 3, 'Color', 'k');
view(3)
grid on

7 Comments
Adam Danz
on 9 Apr 2019
Edited: Adam Danz
on 11 Apr 2019
I edited my answer to show how to plot the origin lines. The xline and yline functions don't work well with 3D plots since their z values are in the middle of the z axis.
To avoid the lines appearing in the legend, either
1) after creating the legend, turn off auto-updating: legend(...,'AutoUpdate','off')
2) plot the legend at the end and specify the handles that should appear in the legend.
Star Strider
on 9 Apr 2019
One option:
zx = 0;
zy = 0;
figure
scatter3(rand(10,1), rand(10,1), rand(10,1))
hold on
xline(0,'--k');
yline(0,'--k');
plot3([1 1]*zx, [1 1]*zy, [zlim], '--k') % Plot ‘zline’ At (zx,zy)
hold off
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!