轴刻度标签在 pgfplot 中无法正确加载

轴刻度标签在 pgfplot 中无法正确加载

我有两个数据系列(实际数据和预测数据)连接在一个点上。然而,标签只加载第一个系列。我需要显示所有日期。以下是 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}

相关内容