pgfplots: 좌표계 끝까지 선을 그리는 방법

pgfplots: 좌표계 끝까지 선을 그리는 방법

여기에 이미지 설명을 입력하세요

y 방향 좌표계의 끝에 정확히 수직선을 그리려면 어떻게 해야 합니까?

·ymax=<value>(자동으로 설정된) 'ymax=10'은 변경될 수 있습니다( 테이블에 무작위 숫자가 포함되어 있기 때문에 잘 모르겠습니다 ).

·x 값 '6'이 제공됩니다(일반적으로 테이블에서 특수 값으로 읽음).

\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}

답변1

\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}

여기에 이미지 설명을 입력하세요

답변2

compat두 가지 아이디어( 어쨌든 옵션을 최근 버전으로 설정해야 함 ):

  1. update limits=false선의 좌표가 y축 제한을 확장하는 것을 방지하려면 다음을 사용하십시오 .
\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. 다음 명령 을 사용하여 선을 그립니다 \draw.
\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}

두 코드 예제 모두 동일한 출력을 생성합니다.

여기에 이미지 설명을 입력하세요

관련 정보