agregar etiquetas y construir la ruta a través de una iteración de lista

agregar etiquetas y construir la ruta a través de una iteración de lista

Tuve que construir un gráfico con metapost para mis clases de física (ver más abajo). Lo que me gustaría preguntarte es si hay alguna manera de agregar la curva y sus etiquetas de alguna manera como lo hago con 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

Respuesta1

No estoy seguro de haber entendido correctamente la pregunta, pero aquí hay un intento de reemplazar la última parte de su código, que se acerca más a la forma en que lo maneja su código 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;

Respuesta2

Aquí hay otro intento (tardío) de reemplazar la última parte de su código. Como antes, utiliza sólo un bucle for, pero éste dibuja las etiquetas después (encima de) la curva, como lo hizo su programa Tikz y como no lo hizo mi programa MetaPost anterior.

%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;

información relacionada