
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 ラベル (具体的には最後のラベル) を配置する方法にあります。
次の例では、この状況を修正する 1 つの方法を示します。 としxtick=data
(これで目盛りとそのラベルがテーブルから自動的に選択されるようになりました)、extra x ticks
とextra xtick labels
を補助的な青いグリッドとともに使用して、18 に対応する点が実際にどこにあるかを示します。また、使用するマークを変更し (使用したマークは大きすぎるため、 にあるような印象を与える可能性があると思います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}