Pgfplots:日期范围和整数作为输入数据

Pgfplots:日期范围和整数作为输入数据

我有一张如下所示的数据表:

Week,value
2010-01-03 - 2010-01-09,0
2010-01-10 - 2010-01-16,0
2010-01-17 - 2010-01-23,0
2010-01-24 - 2010-01-30,0
2010-01-31 - 2010-02-06,0
2010-02-07 - 2010-02-13,0
2010-02-14 - 2010-02-20,0
2010-02-21 - 2010-02-27,0
2010-02-28 - 2010-03-06,0
...

如何包含它来使用 渲染简单的 x/y 图pgfplots?我希望 x 轴上显示日期,y 轴上显示值。

我尝试过这个,但是它不起作用(无法解析日期):

\begin{tikzpicture}
\begin{loglogaxis}[
    title=Trends,
    xlabel={Date},
    ylabel={Searches},
]
\addplot table {data/trends.dat};
\end{loglogaxis}
\end{tikzpicture}

答案1

我同意 Christian 的建议,他在在问题下方评论。有了这个xticklabels from table功能,一切都会变得非常简单。

有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
    \begin{filecontents}{trends.txt}
        Week,value
        2010-01-03 - 2010-01-09,0
        2010-01-10 - 2010-01-16,0
        2010-01-17 - 2010-01-23,0
        2010-01-24 - 2010-01-30,0
        2010-01-31 - 2010-02-06,0
        2010-02-07 - 2010-02-13,0
        2010-02-14 - 2010-02-20,0
        2010-02-21 - 2010-02-27,0
        2010-02-28 - 2010-03-06,0
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.3,
    }
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        % set column separator
        table/col sep=comma,
        % state that you want to set `xtick's at your data points ...
        xtick=data,
        % ... and get the corresponding values from a table
        xticklabels from table={trends.txt}{Week},
        % because the labels are so long, rotate them
        xticklabel style={
            rotate=90,
        },
        title=Trends,
        xlabel={Date},
        ylabel={Searches},
    ]
        \addplot table [
            % use the coordinate index to plot the x value which than matches
            % the `xticklabels from table'
            x expr=\coordindex,
            y=value,
        ] {trends.txt};
    \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容