pgfplots: So zeichnen Sie eine Linie zum Ende des Koordinatensystems

pgfplots: So zeichnen Sie eine Linie zum Ende des Koordinatensystems

Bildbeschreibung hier eingeben

Wie kann ich eine vertikale Linie genau bis zum Ende des Koordinatensystems in y-Richtung zeichnen?

·Beachten Sie, dass sich das (automatisch festgelegte) „ymax=10“ ändern kann (und ich weiß es nicht ymax=<value>, weil die Tabellen zufällige Zahlen enthalten).

·Beachten Sie, dass der x-Wert '6' angegeben ist (normalerweise als Sonderwert aus der Tabelle ausgelesen).

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable


\begin{tikzpicture}[]
\begin{axis}[
xmin=0, ymin=0, 
]
\addplot[mark=*] table[]{\mytable};

\addplot[densely dashed, red] coordinates{(6,0) (6,10)};
\end{axis}
\end{tikzpicture}
\end{document}

Antwort1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable
\begin{tikzpicture}
\begin{axis}[
xmin=0, ymin=0, 
]
\addplot[mark=*] table[]{\mytable};
\draw[red, ultra thick, densely dashed] (6,0) -- (6,0|-current axis.north);
\draw[green, densely dashed] (6,0) -- (6,\pgfkeysvalueof{/pgfplots/ymax});
\end{axis}
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Antwort2

Zwei Ideen (Sie sollten die compatOption auf jeden Fall auf eine aktuelle Eversion setzen):

  1. Verwenden Sie, update limits=falseum zu verhindern, dass die Koordinaten der Linie die Grenzen der Y-Achse überschreiten:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable

\begin{tikzpicture}[]
\begin{axis}[
    xmin=0, ymin=0, 
]
\addplot[mark=*] table[]{\mytable};

\addplot[densely dashed, red, update limits=false] coordinates{(6,0) (6,11)};

\end{axis}
\end{tikzpicture}
\end{document}
  1. Zeichnen Sie die Linie mit einem \drawBefehl:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\pgfplotstableread[col sep=comma]{
0, 0
1, 0
2, 0
3, 1
4, 3
5, 2
6, 5
7, 0
8, 1
12,10
13,5
}\mytable

\begin{tikzpicture}[]
\begin{axis}[
    xmin=0, ymin=0
]
\addplot[mark=*] table[]{\mytable};

\draw[densely dashed, red] 
    (6,0 |- {yticklabel cs:0}) -- (6,0 |- {yticklabel cs:1});

% alternatively:
% \draw[densely dashed, red] 
%    (6,\pgfkeysvalueof{/pgfplots/ymin}) -- (6,\pgfkeysvalueof{/pgfplots/ymax});

\end{axis}
\end{tikzpicture}
\end{document}

Beide Codebeispiele führen zur gleichen Ausgabe:

Bildbeschreibung hier eingeben

verwandte Informationen