How to tick the parameter in the plot of a parametric function?

조회 수: 4 (최근 30일)
Bernhard
Bernhard 2022년 7월 29일
답변: Bernhard 2022년 7월 31일
I have a plot of a parametric function, as in the following simple example:
t = linspace(0, 1, 1000);
x = t.^2;
y = t.^3;
figure
plot(x, y, '-')
axis([0 1 0 1])
axis square
xticks(0:.1:1)
yticks(0:.1:1)
xlabel('x')
ylabel('y')
title('Parametric Plot: x = t^2; y = t^3')
How can I add ticks for the parameter value along the curve, similar to the x- and y-ticks?
Thank you!
Bernhard
  댓글 수: 4
Bernhard
Bernhard 2022년 7월 29일
I mean something like this (three ticks inserted manually):
Adam Danz
Adam Danz 2022년 7월 29일
편집: Adam Danz 2022년 7월 29일
Moving my answer to here in support of the other answers added later.
What about using Grid lines?
t = linspace(0, 1, 1000);
x = t.^2;
y = t.^3;
figure
plot(x, y, '-')
axis([0 1 0 1])
axis square
xticks(0:.1:1)
yticks(0:.1:1)
xlabel('x')
ylabel('y')
title('Parametric Plot: x = t^2; y = t^3')
grid on
Or text objects
figure
plot(x, y, '-')
axis([0 1 0 1])
axis square
xticks(0:.1:1)
yticks(0:.1:1)
xlabel('x')
ylabel('y')
title('Parametric Plot: x = t^2; y = t^3')
idx = 800; %for the 500th point
hold on
plot(x(idx),y(idx), 'bo')
text(x(idx), y(idx), sprintf('[%.2f, %.2f]',x(idx),y(idx)), ...
'VerticalAlignment', 'Bottom', 'HorizontalAlignment', 'right')

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 7월 29일
편집: Voss 2022년 7월 29일
t = linspace(0, 1, 1000);
x = t.^2;
y = t.^3;
tick_length = 0.04;
t_tick = linspace(0,1,21);
x_tick = t_tick.^2;
y_tick = t_tick.^3;
dxdt_tick = 2.*t_tick;
dydt_tick = 3.*t_tick.^2;
theta_tick = atan2(dydt_tick,dxdt_tick)+pi/2;
xx = x_tick+cos(theta_tick)*tick_length/2.*[-1; 1];
yy = y_tick+sin(theta_tick)*tick_length/2.*[-1; 1];
xx(end+1,:) = NaN;
yy(end+1,:) = NaN;
figure
hold on
line(xx(:),yy(:), ...
'Color','k', ...
'LineWidth',1, ...
'Clipping','off')
plot(x, y, '-')
axis([0 1 0 1])
axis square
xticks(0:.1:1)
yticks(0:.1:1)
xlabel('x')
ylabel('y')
title('Parametric Plot: x = t^2; y = t^3')
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Les Beckham
Les Beckham 2022년 7월 29일
Try this (or something similar):
% Your original code
t = linspace(0, 1, 1000);
x = t.^2;
y = t.^3;
figure
plot(x, y, '-')
axis([0 1 0 1])
axis square
xticks(0:.1:1)
yticks(0:.1:1)
xlabel('x')
ylabel('y')
title('Parametric Plot: x = t^2; y = t^3')
% New code
tdisp = [0 .3:.1:1]; % times to mark
hold on
plot(tdisp.^2, tdisp.^3, '+') % mark the points
text(tdisp.^2 + .02, tdisp.^3, split(num2str(tdisp))) % add labels

Bernhard
Bernhard 2022년 7월 31일
Thank you @AdamDanz, @Voss, @LesBeckham for your helpful answers.
Frankly speaking, I had hoped for a neat, fully automated algorithm for adding annotated ticks to the curve, but reflecting on your answers, I see now that this would not be feasable for the general case.
Voss' code, providing ticks orthogonal to the curve, gives the prettiest result, but it depends on calculating the derivatives uf x(t) and y(t), which might be not always possible.
So Les Beckham's proposal of adding '+' Markers, and annotating them with a smart use of the "split" function, might be favorable for most of the practical cases.

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by