add lines through origin in 3D scatter plot

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?

回答 (2 件)

Adam Danz
Adam Danz 2019 年 4 月 9 日
編集済み: Adam Danz 2019 年 4 月 15 日

0 投票

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
190415 124851-Figure 1.jpg

7 件のコメント

Felix Flores James
Felix Flores James 2019 年 4 月 9 日
well, it gives a line at z = 0 if I don't specify the linetype and colour
Star Strider
Star Strider 2019 年 4 月 9 日
Post your code.
When I call zline in scatter3, I get:
Undefined function or variable 'zline'.
This in R2019a.
Adam Danz
Adam Danz 2019 年 4 月 9 日
What's the result of
which zline
Felix Flores James
Felix Flores James 2019 年 4 月 9 日
編集済み: Felix Flores James 2019 年 4 月 9 日
scatter3(O_Synthetic(:,1),O_Synthetic(:,2),O_Synthetic(:,3),'filled','MarkerFaceColor',[0 1 0]); %green
text(O_Synthetic(:,1),O_Synthetic(:,2),O_Synthetic(:,3),OSnames,'FontSize',8);
hold on;
scatter3(O_Natural(:,1),O_Natural(:,2),O_Natural(:,3),'filled','MarkerFaceColor',[1 1 0]); %yellow
text(O_Natural(:,1),O_Natural(:,2),O_Natural(:,3),ONnames,'FontSize',8);
hold on;
scatter3(Cl(:,1),Cl(:,2),Cl(:,3),'filled','MarkerFaceColor',[1 0 0]); %red
text(Cl(:,1),Cl(:,2),Cl(:,3),Clnames,'FontSize',8);
hold on;
scatter3(C(:,1),C(:,2),C(:,3),'filled','MarkerFaceColor',[0 0 0]); %black
text(C(:,1),C(:,2),C(:,3),Cnames,'FontSize',8);
legend(legendname);
xline(0,'--k');
yline(0,'--k');
zline(0);
I have MATLAB version 2018b btw
Felix Flores James
Felix Flores James 2019 年 4 月 9 日
oh you guys are right.
the result of
which zline
gives
C:\Program Files\MATLAB\R2018b\toolbox\PLS_Toolbox_87\zline.p
Felix Flores James
Felix Flores James 2019 年 4 月 9 日
is there a different way to add a z-line? I also would like to know how to remove the lines from my legend
Adam Danz
Adam Danz 2019 年 4 月 9 日
編集済み: Adam Danz 2019 年 4 月 11 日
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
Star Strider 2019 年 4 月 9 日

0 投票

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

カテゴリ

タグ

質問済み:

2019 年 4 月 9 日

編集済み:

2019 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by