我正在尝试制作年度观察值的折线图。X 轴无法正确显示年份;结果显示为 2,008/2,009 等。
以下是我使用的代码:
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel=Year,
ylabel=Number of certificates,
legend pos=north east,
legend entries={Eco,Private},
]
\addplot table [x=Year,y=Eco] {data.txt};
\addplot table [x=Year,y=Private] {data.txt};
\end{axis}
\end{tikzpicture}
\end{figure}
该表位于单独的文件 data.tex 中,格式如下:
X Year Private Eco
1 2007 5877 nan
2 2008 921253 nan
3 2009 6131 nan
4 2010 6953 nan
5 2011 37184 nan
6 2012 61179 nan
7 2013 374902 18
8 2014 565200 12918
9 2015 137686 227964
10 2016 55295 204323
输出结果如下:
我怎样才能让 X 轴上的值显示为年份?
答案1
像这样。对不起,很可能我误解了你的问题。
\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data.txt}
X Year Private Eco
1 2007 5877 nan
2 2008 921253 nan
3 2009 6131 nan
4 2010 6953 nan
5 2011 37184 nan
6 2012 61179 nan
7 2013 374902 18
8 2014 565200 12918
9 2015 137686 227964
10 2016 55295 204323
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Year,
ylabel=Number of certificates,
legend pos=north east,
legend entries={Eco,Private},
xticklabel style={/pgf/number format/1000 sep={}
}
]
\addplot table [x=Year,y=Eco] {data.txt};
\addplot table [x=Year,y=Private] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}