Put arrow and its value in a plot
54 views (last 30 days)
Show older comments
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!
0 Comments
Answers (3)
Mathieu NOE
on 23 Nov 2024 at 11:35
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)])
3 Comments
Mathieu NOE
on 25 Nov 2024 at 10:35
hello again
try this :
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
% define which row for display
value = 24.7; % MLD value to display
[x,~] = find_zc(PTn,z,value);
y = value;
al = 1; % arrow length (in x direction)
arrow([x-al,y],[x,y]); % Fex : https://fr.mathworks.com/matlabcentral/fileexchange/278-arrow
text(x-3*al, y, ['MLD = ' sprintf('%0.5g', y)])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% find x values corresponding to y = threshold (like a zero crossing detector) :
% ZxP is x when signal slope is positive at the crossing point
% ZxN is x when signal slope is negative at the crossing point
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
Mathieu NOE
on 25 Nov 2024 at 10:51
and if you need to plot more than one MLD value, you can use this modified code :
NB that I am not using interp1 because your profile is not monotonic everywhere , so interp1 may fail sometimes.
load('data_ask_MLD')
figure;
plot(PTn,z);
set(gca,'ydir','reverse')
% define which row for display
value = [24.7 33.1 54.7 78.9]; % MLD values to display
al = 1; % arrow length (in x direction)
for k = 1:numel(value)
[x,~] = find_zc(PTn,z,value(k));
x = x(1); % take the first value
arrow([x-al,value(k)],[x,value(k)]); % Fex : https://fr.mathworks.com/matlabcentral/fileexchange/278-arrow
text(x-3*al, value(k), ['MLD = ' sprintf('%0.5g', value(k))])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% find x values corresponding to y = threshold (like a zero crossing detector) :
% ZxP is x when signal slope is positive at the crossing point
% ZxN is x when signal slope is negative at the crossing point
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
Pramil
on 23 Nov 2024 at 11:45
Edited: Pramil
on 23 Nov 2024 at 11:48
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
on 23 Nov 2024 at 15:57
Edited: Star Strider
on 23 Nov 2024 at 19:45
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
zval = interp1(PTn, z, PTnval) % Y-Coordinate Value (Derived From Data)
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'.
.
4 Comments
Star Strider
on 26 Nov 2024 at 14:04
My pleasure!
It appears that my ‘kludge’ fix is the best option just now. I have not checked to see if it might work in other instances, however I believe it is likely a ‘one-off’ that will not generalise to other situations.
My only consolation is that my code works correctly, and would continue to do so if the absolute references changed with the axis direction (and possibly axis scaling, however I have not experimented with it with a diifferent axiis scale).
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!