Put arrow and its value in a plot

23 views (last 30 days)
Adi Purwandana
Adi Purwandana about 16 hours ago
Edited: Star Strider about 7 hours ago
Hello there,
I want to show a row and its value in a plot. Please find attached the data. My intended plot should be like this:
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
Thank you!

Answers (3)

Mathieu NOE
Mathieu NOE about 11 hours ago
hello
this is a simple example , based on the fex submission :
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
% define which row for display
r = 100;
x = PTn(r);
y = z(r);
al = 1; % arrow length (in x direction)
arrow([x-al,y],[x,y]); % Fex : https://fr.mathworks.com/matlabcentral/fileexchange/278-arrow
text(x-4*al, y, ['MLD = ' sprintf('%0.5g', y)])

Pramil
Pramil about 11 hours ago
Edited: Pramil about 11 hours ago
Hi Adi,
You can use the "text" function available in MATLAB to create plot shown in your image. Here is the documentation link for the same for your reference:
And here is the updated code:
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
[~, idx] = min(abs(z - MLD));
x_value = PTn(idx);
text(x_value, MLD, 'MLD = 24.7\rightarrow ','HorizontalAlignment','right');

Star Strider
Star Strider about 7 hours ago
Edited: Star Strider about 3 hours ago
The 'textarrow' coordinates have to adapt to the data and then correct for the reversed y-axis direction.
Try this —
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
xlabel('PTn')
ylabel('z')
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
MLDval = 24.7;
PTnval = interp1(z, PTn, MLDval) % X-Coordinate Value (Derived From Data) Right End Of The Arrow
PTnval = 27.2905
zval = interp1(PTn, z, PTnval) % Y-Coordinate Value (Derived From Data)
zval = 24.7000
annotation('textarrow', xapf(PTnval+[-2.25 -0.25],pos,xl), 1-yapf([1 1]*zval,pos,yl), String=("MLD = "+MLDval)+" m ", HeadStyle='vback3')
EDIT — (23 Nov 2024 at 12:45)
Changed 'HeadStyle'.
.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!