フィルターのクリア

How do use the annotation function to annotate specific x-values on my graph?

14 ビュー (過去 30 日間)
Matthew
Matthew 2023 年 9 月 30 日
コメント済み: Star Strider 2023 年 10 月 5 日
I want to use the annotation function to show x-min and x-max values on my graph. Here is my code:
x = [0:300];
lda = 300;
uhat = 10;
k = ((2*pi)/(lda));
phi1 = 0;
advection1 = uhat*((cos((k*x)-(phi1))));
xmaxadvection1 = ((0)+phi1)/(k);
xminadvection1 = ((pi)+phi1)/(k);
figure(1)
plot(x,advection1)
xlabel('x (km)')
ylabel('advection')

採用された回答

Star Strider
Star Strider 2023 年 9 月 30 日
I wrote a couple anonymous functions recently to transform (x,y) coordinates to normalised coordinates for just this purpose.
Try this —
x = [0:300];
lda = 300;
uhat = 10;
k = ((2*pi)/(lda));
phi1 = 0;
advection1 = uhat*((cos((k*x)-(phi1))));
xmaxadvection1 = ((0)+phi1)/(k);
xminadvection1 = ((pi)+phi1)/(k);
figure(1)
plot(x,advection1)
xlabel('x (km)')
ylabel('advection')
pos = gca().Position;
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
annotation('textarrow', xapf([15 0]+xmaxadvection1,pos,xlim), yapf([-8 -10],pos,ylim), 'String','x_{max}')
annotation('textarrow', xapf([1 1]*xminadvection1,pos,xlim), yapf([-8 -10],pos,ylim), 'String','x_{min}')
The first arguments to the functions are the appropriate vectors that define the arrows, the second is the axis position vector, and the third are the axis limits. (Making them more intuitive is difficult.) Make appropriate changes to get the result you want.
.
  6 件のコメント
Matthew
Matthew 2023 年 10 月 5 日
Sorry about this but I have one more question when using this annotation function. I am getting errors that X and Y values should be between 0 and 1. How would I do it with this code? Thanks again.
x = [0:300]*10^3;
That = 7.5; % in Kelvin
lda = 300*10^3; % in km
k = ((2*pi)/(lda)); % finding k
uhat = 10; % in m/s
phi1 = 0;
phi2 = pi/4;
phi3 = pi/2;
advection1 = -((uhat)*sin((k.*x)-phi1))*(-That)*(k).*(cos(k*x));
lbda2 = 300;
k1 = ((2*pi)/(lbda2)); % in km
xmaxadvection1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi)/(2*k1));
xminadvection1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*2)/(2*k1));
xmaxadvection_1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*3)/(2*k1));
xminadvection_1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*4)/(2*k1));
figure(3)
plot(x/1000,advection1)
Star Strider
Star Strider 2023 年 10 月 5 日
No worries! The annotation funcitons are not intuitive, and (unfortunately) neither are my functions to transform the (xy) coordinates to normalised values to work with them.
Until I looked closer, I did not realise that you were dividing ‘x’ by 1000 in the plot, so I was getting some anomalous values for ‘yxmax’ and ‘yxmin’ (interpolation to find the corresponding y-values for the x-values). I defined ‘ofst’ as one-tenth the ylim difference, governing the lengths of the arrows. Change that if you want to.
Also, if you want different names for the annotations (other than and ) you can put those into a cell array or a string array and then subscript them appropriately as the value in the 'String' name-value pair in the annotation calls in the loop that plots them. No other changes should be necessary to get them to display correctly.
This should work —
x = [0:300]*10^3;
That = 7.5; % in Kelvin
lda = 300*10^3; % in km
k = ((2*pi)/(lda)); % finding k
uhat = 10; % in m/s
phi1 = 0;
phi2 = pi/4;
phi3 = pi/2;
advection1 = -((uhat)*sin((k.*x)-phi1))*(-That)*(k).*(cos(k*x));
lbda2 = 300;
k1 = ((2*pi)/(lbda2)); % in km
xmaxadvection1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi)/(2*k1));
xminadvection1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*2)/(2*k1));
xmaxadvection_1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*3)/(2*k1));
xminadvection_1 = ((phi1)/(2*k1)) - ((pi)/(4*k1)) + ((pi*4)/(2*k1));
xmax = [xmaxadvection1; xmaxadvection_1]; % Combine Into One Vector For Efficiency
yxmax = interp1(x/1000, advection1,xmax); % Interpolate To Find 'y' Coordinate
xmin = [xminadvection1; xminadvection_1];
yxmin = interp1(x/1000, advection1,xmin);
figure(3)
plot(x/1000,advection1)
pos = gca().Position;
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
ofst = diff(ylim)/10; % Used In 'text' Calls
for k = 1:numel(xmax)
annotation('textarrow', xapf([0 0]+xmax(k),pos,xlim), yapf(-[ofst 0]+yxmax(k),pos,ylim), 'String','x_{max}')
end
for k = 1:numel(xmin)
annotation('textarrow', xapf([0 0]+xmin(k),pos,xlim), yapf([ofst 0]+yxmin(k),pos,ylim), 'String','x_{min}')
end
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by