由于我是 Latex 的初学者,我无法在 pgfplots 的数学模式中使用希腊符号 \pi。我总是收到错误。
\documentclass[12pt,english]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{example.csv}
t,$\pi$,$y$
1972,1.65,1.28
1973,3.14,1.56
1974,3.28,2.24
1975,3.04,2.50
\end{filecontents*}
\clearpage
\begin{document}
\begin{figure}[H]
\centering
\resizebox{0.75\textwidth}{.45\textwidth}{%
\begin{tikzpicture}
\begin{axis}[legend entries={$\pi$, $y$},
legend pos=north west,xlabel=Years,
ylabel=Log]
\addplot table [x=t, y=$\pi$, col sep=comma] {example.csv};
\addplot table [x=t, y=$poil_{t}^{d}$, col sep=comma] {example.csv};
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}
答案1
错误不是由 引起的\pi
,legend entries
而是由\pi
CSV 文件头中的 引起的。只需在标题行中不使用 (LaTeX) 命令即可生成一个可运行的示例。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{example.csv}
% don't use any (LaTeX) commands in the header row
% simply replacing `pi' by `\pi' here (and commenting the first
% `\addplot' command) will raise an error
t,pi,y
1972,1.65,1.28
1973,3.14,1.56
1974,3.28,2.24
1975,3.04,2.50
\end{filecontents*}
\usepackage{pgfplots}
% use this `compat' level or higher to make use of the "advanced"
% axis label positioning
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% using `$\pi$' here is not a problem
legend entries={$\pi$, $y$},
legend pos=north west,
xlabel=Years,
ylabel=Log,
% don't show half years ...
xtick distance=1,
xticklabel style={
% ... and don't use a comma as thousand separator for the years
/pgf/number format/set thousands separator={},
},
% % if you want to show additional ticks without labels ...
% minor x tick num=1,
table/col sep=comma,
]
\addplot table [x=t, y=pi] {example.csv};
\addplot table [x=t, y=y] {example.csv};
\end{axis}
\end{tikzpicture}
\end{document}