![라벨을 추가하고 목록 반복을 통해 경로를 구축하세요.](https://rvso.com/image/281480/%EB%9D%BC%EB%B2%A8%EC%9D%84%20%EC%B6%94%EA%B0%80%ED%95%98%EA%B3%A0%20%EB%AA%A9%EB%A1%9D%20%EB%B0%98%EB%B3%B5%EC%9D%84%20%ED%86%B5%ED%95%B4%20%EA%B2%BD%EB%A1%9C%EB%A5%BC%20%EA%B5%AC%EC%B6%95%ED%95%98%EC%84%B8%EC%9A%94..png)
저는 물리 수업을 위해 메타포스트를 사용하여 그래프를 작성해야 했습니다(아래 참조). 제가 묻고 싶은 것은 tikz에서 수행하는 방식으로 곡선과 해당 레이블을 추가하는 방법이 있는지입니다.
\draw[color=red, ultra thick] (0,0) \foreach \x/\y/\text in {1/1/t=1 s, 2/2/t=2 s, 4/6/t=3 s, 6/8/t=4 s,9/8/t=5 s, 11/6/t=6 s, 13/5/t=7 s}
{
-- (\x,\y) node[above]{\small \text}
};
\startMPcode
path p;
numeric xmin, xmax, ymin, ymax;
ux := .5cm; uy := .5cm;
xmin := 0; xmax := 14;
ymin := 0; ymax := 10;
% draw tickmarks and labels on horizontal axis
for i=0 upto xmax:
draw (i,-0.05)*ux--(i,ymax)*ux withcolor 0.8white;
draw (i,-0.05)*ux--(i,0.05)*ux;
label.bot(textext(decimal(i*10)) scaled 0.7,(i,0)*ux);
endfor;
% draw tickmarks and labels on vertical axis
for i=0 upto ymax:
draw (-0.05,i)*uy--(xmax,i)*uy withcolor 0.8white;
draw (-0.05,i)*uy--(0.05,i)*uy;
label.lft(textext(decimal(i*10)) scaled 0.7,(0,i)*uy);
endfor;
%draw the axis
drawoptions(withcolor black);
drawarrow (xmin,0)*ux -- (xmax+1/2,0)*ux;
drawarrow (0,ymin)*uy -- (0,ymax+1/10)*uy;
label.bot(btex $x$ (m) etex scaled .7, (xmax*ux,-0.8*uy));
label.lft(btex $y$ (m) etex rotated(90) scaled .7, (-0.8*ux,ymax*uy));
%draw the curve and labels
pickup pencircle scaled 2pt;
p:=(0,0)--(1*ux,1*uy)--(2*ux,2*uy)--(4*ux,6*uy)--(6*ux,8*uy)--(9*ux,8*uy)--(11*ux,6*uy)--(13*ux,5*uy);
draw p withcolor red ;
dotlabel.top(btex t=1 s etex scaled 0.7, (1*ux,1*uy)) withcolor blue;
dotlabel.top(btex t=2 s etex scaled 0.7, (2*ux,2*uy)) withcolor blue;
dotlabel.top(btex t=3 s etex scaled 0.7, (4*ux,6*uy)) withcolor blue;
dotlabel.top(btex t=4 s etex scaled 0.7, (6*ux,8*uy)) withcolor blue;
dotlabel.top(btex t=5 s etex scaled 0.7, (9*ux,8*uy)) withcolor blue;
dotlabel.top(btex t=6 s etex scaled 0.7, (11*ux,6*uy)) withcolor blue;
dotlabel.top(btex t=7 s etex scaled 0.7, (13*ux,5*uy)) withcolor blue;
\stopMPcode
답변1
질문을 올바르게 이해했는지 확실하지 않지만 다음은 tikz 코드가 처리하는 방식에 더 가까운 코드의 마지막 부분을 대체하려는 시도입니다.
%draw the curve and labels
pickup pencircle scaled 2pt;
i:=0;
p:=(0, 0)
for k=(1, 1), (2, 2), (4, 6), (6, 8), (9, 8), (11, 6), (13, 5):
-- (xpart(k)*ux, ypart(k)*uy)
hide(i:=i+1; dotlabel.top(textext("t=" & decimal(i) &" s"), (xpart(k)*ux, ypart(k)*uy)) withcolor blue;)
endfor;
draw p withcolor red;
답변2
다음은 코드의 마지막 부분을 교체하려는 또 다른 (늦은) 시도입니다. 이전과 마찬가지로 단 하나의 for 루프만 사용하지만 Tikz 프로그램이 그랬고 이전 MetaPost 프로그램이 그랬던 것처럼 곡선 뒤에(맨 위에) 레이블을 그립니다.
%draw the curve and labels
pickup pencircle scaled 2pt;
picture mylabels; mylabels:=nullpicture;
i := 0;
p := (0, 0)
for k = (1, 1), (2, 2), (4, 6), (6, 8), (9, 8), (11, 6), (13, 5):
-- k xyscaled (ux, uy)
hide(i:=i+1;
addto mylabels also
thelabel.top(textext("t="&decimal(i)&" s"), k xyscaled(ux, uy)) withcolor blue)
endfor;
draw p withcolor red;
addto currentpicture also mylabels;