
pgfplot을 사용하여 일부 데이터를 플롯하고 싶습니다. 내가 작성한 코드는 다음과 같습니다.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={legend pos = north west, font=\footnotesize, draw=none},
legend cell align=left,
xtick=data,
ytick={0,5,10,...,45},
xticklabels={0,2,4,...,16,18},
every axis label={font=\footnotesize},
tick label style={font=\footnotesize},
label style={font=\footnotesize},
enlarge x limits=false,
enlarge y limits=false]
\addplot[lightgray, mark=*] table [x expr = \lineno, y = Time] {Data.dat};
\addlegendentry{x}
\end{axis}
\end{tikzpicture}
\end{document}
Data.dat 파일의 내용은 다음과 같습니다.
Points Time
0 0
2000 1.11
4000 3.54
6000 7.35
8000 12.43
10000 18.15
12000 24.51
14000 31.52
16000 38.59
17791 45.35
이 사이트를 처음 사용하기 때문에 출력 그래프를 게시할 수 없습니다. 출력 그래프에서 마지막 점(17791, 45.35)은 x=18 및 y=45.35에 잘못 표시되었습니다.
제 질문은: x=16과 x=18 지점 사이에 마지막 지점을 올바르게 표시하려면 어떻게 해야 합니까?
답변1
점은 x=17.791
, y=45.35
; 문제는 마지막 틱에 사용된 레이블이 잘못되었다는 것입니다(수동으로 18로 선언했으며 이것이 기본값을 재정의합니다 17.8
). 문제는 플롯이 아닙니다. 문제는 x 레이블(특히 마지막 레이블)을 배치하는 방식입니다.
다음 예에서는 상황을 수정할 수 있는 한 가지 가능한 방법을 보여줍니다. xtick=data
이제 눈금과 해당 레이블이 테이블에서 자동으로 선택되고 보조 파란색 그리드를 사용하여 extra x ticks
18 extra xtick labels
에 해당하는 점이 실제로 어디에 있는지 표시합니다. ; 또한 사용된 마크를 변경했으며(제 생각에는 사용한 마크가 너무 커서 에 있는 듯한 느낌을 줄 수 있습니다 x=18
) 시각화 목적으로만 다른 색상을 사용했습니다. 보시다시피, 마지막 점의 x 좌표는 정확히 있어야 할 위치입니다( 에 대해 그려진 빨간색 수직선의 약간 왼쪽 x=18
).
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
xmax=18500,
legend style={legend pos = north west, font=\footnotesize, draw=none},
legend cell align=left,
xtick=data,
ytick={0,5,10,...,45},
extra x ticks={18000},
extra x tick labels={},
extra x tick style={grid=major,tick label style={rotate=90,anchor=east}},
major grid style={color=red},
every axis label={font=\footnotesize},
tick label style={font=\footnotesize},
label style={font=\footnotesize},
enlarge x limits=false,
enlarge y limits=false,
scaled x ticks=base 10:-3
]
\addplot[blue, mark=x] table {Data.dat};
\addlegendentry{x}
\end{axis}
\end{tikzpicture}
\end{document}
다음에 대해 명시적인 목록을 사용할 수 있습니다 xmark
.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[
xmax=18500,
ymax=46,
legend style={legend pos = north west, font=\footnotesize, draw=none},
legend cell align=left,
xtick={0,2000,4000,6000,8000,...,14000,16000,18000},
ytick={0,5,10,...,45},
major grid style={color=red},
every axis label={font=\footnotesize},
tick label style={font=\footnotesize},
label style={font=\footnotesize},
enlarge x limits=false,
enlarge y limits=false,
scaled x ticks=base 10:-3
]
\addplot[blue, mark=x] table {Data.dat};
\addlegendentry{x}
\end{axis}
\end{tikzpicture}
\end{document}