![轴刻度标签在 pgfplot 中无法正确加载](https://linux22.com/image/324867/%E8%BD%B4%E5%88%BB%E5%BA%A6%E6%A0%87%E7%AD%BE%E5%9C%A8%20pgfplot%20%E4%B8%AD%E6%97%A0%E6%B3%95%E6%AD%A3%E7%A1%AE%E5%8A%A0%E8%BD%BD.png)
我有两个数据系列(实际数据和预测数据)连接在一个点上。然而,标签只加载第一个系列。我需要显示所有日期。以下是 MWE:
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{pgfplots,tikz}
\RequirePackage{filecontents}
\begin{filecontents}{Data1.csv}
Period,spot,forecast
jun 15,1.93,
jul 15,2.12,
ago 15,2.23,
sep 15,1.81,
oct 15,1.76,
nov 15,1.18,
dic 15,1.12,1.12
ene 16,,0.80
feb 16,,0.88
mar 16,,0.84
abr 16,,0.93
may 16,,1.03
jun 16,,1.55
\end{filecontents}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{table/col sep = comma}
\begin{tikzpicture}
\begin{axis}[
xticklabels from table={Data1.csv}{Period},
xtick=data,
xticklabel style={rotate=45},
]
\addplot[smooth] table [x expr=\coordindex, y=spot, col sep = comma] {Data1.csv};
\addplot[dashed,red] table [x expr=\coordindex, y=forecast, col sep = comma] {Data1.csv};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
xtick=data
仅使用第一个命令的数据\addplot
。对于您的情况,您可以使用xtick={0,...,100}
(或其他较大的上限值):
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{pgfplots,tikz}
\RequirePackage{filecontents}
\begin{filecontents}{Data1.csv}
Period,spot,forecast
jun 15,1.93,
jul 15,2.12,
ago 15,2.23,
sep 15,1.81,
oct 15,1.76,
nov 15,1.18,
dic 15,1.12,1.12
ene 16,,0.80
feb 16,,0.88
mar 16,,0.84
abr 16,,0.93
may 16,,1.03
jun 16,,1.55
\end{filecontents}
\begin{document}
\begin{figure}
\centering
\pgfplotsset{table/col sep = comma}
\begin{tikzpicture}
\begin{axis}[
xticklabels from table={Data1.csv}{Period},
xtick={0,...,100},
xticklabel style={rotate=45, anchor=north east, yshift=0.5ex},
enlarge x limits={abs=0.5}
]
\addplot[smooth] table [x expr=\coordindex, y=spot, col sep = comma] {Data1.csv};
\addplot[dashed,red] table [x expr=\coordindex, y=forecast, col sep = comma] {Data1.csv};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}