
How to add an arrow at the end of plot precisely using annotation
26 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am trying to create a 3D scatter plot with matlab with arrows indicating the increasing direction and with a text. So far I've fully created a 3D plot and got rid of the ticks as I wanted by using this code
clc;clear;
y = [1 0 -1];
x = [0 1 -1];
z = [0 1 -1];
figure()
hAxis = axes;
scatter3(1,0,0, 80, 'MarkerEdgeColor','k',...
'MarkerFaceColor', [255 0 0]/255)
hold on
scatter3(-1,-1,-1, 80, 'MarkerEdgeColor','k',...
'MarkerFaceColor', [0 255 0]/255)
scatter3(0,1,1,80, 'MarkerEdgeColor','k',...
'MarkerFaceColor', [0 0 255]/255)
view(115,10)
set(gca, 'xtick', [], 'ytick', [], 'ztick', [])
ylim([-2 2])
xlim([-2 2])
zlim([-2 2])
hAxis.XRuler.FirstCrossoverValue = 0; % X crossover with Y axis
hAxis.YRuler.FirstCrossoverValue = 0; % Y crossover with X axis
hAxis.ZRuler.FirstCrossoverValue = 0; % Z crossover with X axis
hAxis.ZRuler.SecondCrossoverValue = 0; % Z crossover with Y axis
hAxis.XRuler.SecondCrossoverValue = 0; % X crossover with Z axis
hAxis.YRuler.SecondCrossoverValue = 0; % Y crossover with Z axis
hAxis.LineWidth = 2;
This generates the following figure

While I want something like this (ideally with the text aligned with the axis)

I know I can use annotation, but is very very frustrating to make it work. Any suggestions to a more precise way to do this?
0 件のコメント
回答 (1 件)
Samayochita
2025 年 2 月 11 日 6:09
Hi Guilherme,
You can achieve this using “quiver3” (https://www.mathworks.com/help/matlab/ref/quiver3.html) for the arrows and “text” (https://www.mathworks.com/help/matlab/ref/text.html) for labelling the axes.
The advantage of “quiver3” is that it allows precise placement of arrows in 3D space without relying on annotations, which are figure-based and often misaligned in 3D plots.
The “text” function ensures that the labels are placed slightly beyond the end of each axis arrow to avoid overlap. You could add the below code to the code you have already written:
% Arrows for axes
quiver3(0,0,0, 1.5,0,0, 'r', 'LineWidth', 2, 'MaxHeadSize', 0.3) % X-axis
quiver3(0,0,0, 0,1.5,0, 'g', 'LineWidth', 2, 'MaxHeadSize', 0.3) % Y-axis
quiver3(0,0,0, 0,0,1.5, 'b', 'LineWidth', 2, 'MaxHeadSize', 0.3) % Z-axis
% Labels for axes
text(1.7, 0, 0, 'X', 'FontSize', 12, 'Color', 'r', 'FontWeight', 'bold')
text(0, 1.7, 0, 'Y', 'FontSize', 12, 'Color', 'g', 'FontWeight', 'bold')
text(0, 0, 1.7, 'Z', 'FontSize', 12, 'Color', 'b', 'FontWeight', 'bold')
grid on
Hope this helps!

0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!