Beschriftungen hinzufügen und den Pfad durch eine Listeniteration erstellen

Beschriftungen hinzufügen und den Pfad durch eine Listeniteration erstellen

Ich musste für meinen Physikunterricht ein Diagramm mit Metapost erstellen (siehe unten). Ich möchte Sie fragen, ob es eine Möglichkeit gibt, die Kurve und ihre Beschriftungen so hinzuzufügen, wie ich es mit Tikz mache:

\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

Antwort1

Ich bin nicht sicher, ob ich die Frage richtig verstanden habe, aber hier ist ein Versuch, den letzten Teil Ihres Codes zu ersetzen, der der Art und Weise, wie Ihr Tikz-Code damit umgeht, näher kommt:

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

Antwort2

Hier ist ein weiterer (später) Versuch, den letzten Teil Ihres Codes zu ersetzen. Wie zuvor wird nur eine For-Schleife verwendet, aber diese zeichnet die Beschriftungen nach (über) der Kurve, wie es Ihr Tikz-Programm tat und mein vorheriges MetaPost-Programm nicht.

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

verwandte Informationen