Put arrow and its value in a plot

10 vues (au cours des 30 derniers jours)
Adi Purwandana
Adi Purwandana le 23 Nov 2024
Commenté : Star Strider le 26 Nov 2024
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!

Réponses (3)

Mathieu NOE
Mathieu NOE le 23 Nov 2024
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 commentaires
Mathieu NOE
Mathieu NOE le 25 Nov 2024
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
Mathieu NOE le 25 Nov 2024
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

Connectez-vous pour commenter.


Pramil
Pramil le 23 Nov 2024
Modifié(e) : Pramil le 23 Nov 2024
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');
  1 commentaire
Adi Purwandana
Adi Purwandana le 25 Nov 2024
Thank you @Pramil; but since I have lots of profiles, I need an automatically adjusted the MLD value.

Connectez-vous pour commenter.


Star Strider
Star Strider le 23 Nov 2024
Modifié(e) : Star Strider le 23 Nov 2024
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'.
.
  4 commentaires
Adi Purwandana
Adi Purwandana le 26 Nov 2024
Great! Thanks for let us know the update.
Star Strider
Star Strider le 26 Nov 2024
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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by