
(처음으로) pgfplot을 사용하여 일종의 가로 막대가 있는 차트를 만들려고 합니다. 그림을 참조하세요.
어떻게 해야 하는지는 알지만 코드가 꽤 장황하고 데이터가 많기 때문에 \labeledRange{beginpos}{endpos}{label}
. 또한 수직 위치를 수동으로 지정하는 것을 없애기 위해 카운터( )를 사용했습니다 vertposition
(각 범위에 대해 증가시키길 원함). 그러나 \node
아래 코드의 명령 에서 카운터를 사용할 때 카운터가 예상대로 작동하지 않습니다 . 위 그림에서 라벨 LABEL1
과 는 LABEL 2
각각 파란색과 빨간색 막대 근처에 있어야 합니다.
어떻게 하면 작동하게 할 수 있나요?
(그리고 또 다른 문제로 '핀' 라인을 어떻게 제거할 수 있나요?)
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
%\labeledRange{start}{end}{label}
\newcounter{vertposition}
\newcommand{\labeledRange}[3]{
\addplot coordinates {(#1,\arabic{vertposition}) (#2,\arabic{vertposition})};
\node[coordinate, pin=right:{#3}]
at (axis cs:#2,\arabic{vertposition}) {};
\stepcounter{vertposition}
}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=100] %,ytick=\empty]
%using my macro -> label position is wrong
\labeledRange{10}{20}{LABEL 1}
\labeledRange{60}{70}{LABEL 2}
%without macro -> works fine
\addplot coordinates {(20,3) (50,3)};
\node[coordinate, pin=right:{LABEL 3}]
at (axis cs:50,3) {};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
답변1
및 환경 내에서 \node
같은 TikZ 명령은 즉시 실행되지 않지만 모든 플롯이 완료된 후에 수집되어 실행됩니다. 이는 좌표계와 같은 작업을 수행하는 데 필요합니다 (나중 플롯에서 여전히 축 범위를 변경할 수 있으므로 좌표계는 모든 플롯이 지정될 때까지 고정되지 않습니다). 이로 인해 모든 라벨은 동일한 카운터의 마지막 값을 사용합니다.\draw
\path
axis
axis cs:
별도의 명령을 사용하는 대신 명령 앞에 ( 없이 )를 \node
삽입할 수 있습니다 . 이렇게 하면 노드가 자동으로 플롯의 끝에 배치됩니다.node ...
\
;
\addplot
얇은 선은 를 사용하여 발생합니다 pin
. 대신 연결선 없이 label
작동하는 를 사용할 수 있습니다 . 또는 이 경우에는 and를 모두 pin
사용하지 않고 직접 사용하는 것이 훨씬 좋습니다.pin
label
node
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
%\labeledRange{start}{end}{label}
\newcounter{vertposition}
\newcommand{\labeledRange}[3]{
\addplot coordinates {(#1,\arabic{vertposition}) (#2,\arabic{vertposition})} node [black,anchor=west] {#3};
\stepcounter{vertposition}
}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=100] %,ytick=\empty]
%using my macro
\labeledRange{10}{20}{LABEL 1}
\labeledRange{60}{70}{LABEL 2}
%without macro
\addplot coordinates {(20,3) (50,3)};
\node[coordinate, label=right:{LABEL 3}]
at (axis cs:50,3) {};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}