3

I have a data table that looks something like:

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
...

How do I include this to render a simple x/y plot using pgfplots? I'd like the dates on the x-axis and the values on the y-axis.

I've tried this, but it is not working (fails to parse the dates):

\begin{tikzpicture}
\begin{loglogaxis}[
    title=Trends,
    xlabel={Date},
    ylabel={Searches},
]
\addplot table {data/trends.dat};
\end{loglogaxis}
\end{tikzpicture}
Jesse
  • 29,686
Mickel
  • 133
  • These two questions might be related to your problem: http://tex.stackexchange.com/questions/89138/trend-line-with-pgfplots-and-dateplots and http://tex.stackexchange.com/questions/9733/pgfplot-datafile-format-for-datetime-field – SFAB Feb 03 '14 at 13:05
  • I think the best way would be to plot the row index versus value, i.e. you use \addplot table[x expr=\coordindex]. Perhaps there is a suitable way to show which week corresponds to which index? ... and the x axis should have a linear scale, I suppose. – Christian Feuersänger Feb 06 '14 at 18:54

1 Answers1

2

I agree with the suggestions from Christian he stated in the comment below the question. Together with the xticklabels from table feature then everything is quite easy.

For more details have a look at the comments in the code.

% 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}

image showing the result of above code

Stefan Pinnow
  • 29,535