绘制折线图

绘制折线图

由于未知原因,代码无法编译。

    \begin{figure}
        \centering
        \begin{tikzpicture}
            \begin{axis}[
                xlabel=Month,
                ylabel=Rainfall (mm),
                ymin=80,
                ymax=450,
                date coordinates in=x,
                xticklabel style={rotate=45, anchor=near xticklabel},
                xticklabel={\month},
                date ZERO=2021.12.31, % reference date
                xtick={
                    2022-01-01,
                    2022-02-01,
                    2022-03-01,
                    2022-04-01,
                    2022-05-01,
                    2022-06-01,
                    2022-07-01,
                    2022-08-01,
                    2022-09-01,
                    2022-10-01,
                    2022-11-01,
                    2022-12-01
                },
                xticklabel style={/pgf/number format/1000 sep=,rotate=45,anchor=north east},
            ]
                \addplot coordinates {
                    (2022-01-01, 81.9)
                    (2022-02-01, 70)
                    (2022-03-01, 104.9)
                    (2022-04-01, 276.2)
                    (2022-05-01, 311.6)
                    (2022-06-01, 201.5)
                    (2022-07-01, 132.5)
                    (2022-08-01, 108.5)
                    (2022-09-01, 248.8)
                    (2022-10-01, 399.1)
                    (2022-11-01, 362.4)
                    (2022-12-01, 165.1)
                };
            \end{axis}
        \end{tikzpicture}
        \caption{Average Monthly Total Rainfall}
    \end{figure}

答案1

看起来你已经接近目标了。请查看以下我的更改:

  • 使用standalone,在开发图形时更加温和(稍后切换回来)
  • 注释掉里面的几乎所有内容axis,直到编译时没有错误(和空内容)
  • 包含所述内容的行2022-08-01,它无法处理8(以及此后的内容)
  • 添加\usepgfplotslibrary{dateplot},重新输入所述数据线
  • 最后编译的代码如下所示

因此仍需微调。我建议逐步删除注释(=删除行)并进行编译,以确保它仍能编译,并且一切朝着正确的方向发展。

这样,当问题发生时,您就可以定位问题(有时禁用这些线路并将解决方案留到以后是一个好主意......无论如何,通常它们已经消失了)。

结果

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}% <<< perhaps you used it already?

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
%            xlabel=Month,
%            ylabel=Rainfall (mm),
%            ymin=80,
%            ymax=450,
            date coordinates in=x,% <<< was necessary
%            xticklabel style={rotate=45, anchor=near xticklabel},
%            xticklabel={\month},
%            date ZERO=2021.12.31, % reference date
%            xtick={
%                2022-01-01,
%                2022-02-01,
%                2022-03-01,
%                2022-04-01,
%                2022-05-01,
%                2022-06-01,
%                2022-07-01,
%                2022-08-01,
%                2022-09-01,
%                2022-10-01,
%                2022-11-01,
%                2022-12-01
%            },
%            xticklabel style={/pgf/number format/1000 sep=,rotate=45,anchor=north east},
        ]
            \addplot coordinates {
                (2022-01-01, 81.9)
                (2022-02-01, 70)
                (2022-03-01, 104.9)
                (2022-04-01, 276.2)
                (2022-05-01, 311.6)
                (2022-06-01, 201.5)
                (2022-07-01, 132.5)
                (2022-08-01, 108.5)
                (2022-09-01, 248.8)
                (2022-10-01, 399.1)
                (2022-11-01, 362.4)
                (2022-12-01, 165.1)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容