필터 지우기
필터 지우기

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

조회 수: 14 (최근 30일)
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 CenterFile Exchange에서 Specifying 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