0

Depending on this question how to plot regression on DATE x-axes I post the MWE from Christian Feuersänger:

\documentclass{minimal}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{filecontents}{data.dat}
date            value1
2010-01-01      2
2010-01-02      10
2010-01-03      8
2010-01-04      15
\end{filecontents}
\usepackage{pgfplotstable}

% formats row \ticknum of the input table using '#3'
% #1: table name
% #2: column name
% #3: formatting instruction like \day.\month.\year
    \newcommand\datelabelsfromtable[3]{%
    \begingroup
    \countdef\result=\count200 %
    \pgfplotstablegetelem{\ticknum}{#2}\of{#1}%
    \pgfcalendardatetojulian{\pgfplotsretval}{\result}%
    \pgfcalendarjuliantodate{\result}\year\month\day
    %
    #3%
    \endgroup
}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west},
xticklabel={\datelabelsfromtable{data.dat}{date}{\day.\month.\year}},
xtick=data,
]
\addplot table [x expr=\coordindex,y=value1] {data.dat};
\addplot table [x expr=\coordindex, y={create col/linear regression={y=value1}}] {data.dat};
\end{axis}

\end{tikzpicture}
\end{document}

The regression-line on this works and I hope I understood his command. Using my data I want that PGF uses all of my specified data-points inside the .dat file. However, I don't want to use

xtick=data,

But I would like to specify for which data points the labels are written as the first half of my graph is not so important but at the end i would like to have more ticks. Using the exmaple data from the MWE, I would think of something lise this , skipping 2010-01-02:

xtick={2010-01-01, 2010-01-03, 2010-01-04}

I tried different versions of this just like {2010} but always receive an error (maybe because the x dates are not declared as DATE? Doing so also result an in error).

So to sum it up:

How can I use the approach from Feuersänger (or if someone has another idea for using Dates when doing regression) but can specify which ticks are printed and not just using xtick=data?

1

Using the code from https://tex.stackexchange.com/a/372049/95441 (as suggested also in your parallel question) using xtick there works perfectly fine ...

% used PGFPlots(Table) v1.16
% (code from <https://tex.stackexchange.com/a/372049/95441>)
\begin{filecontents*}{testtable.csv}
date;value
2017-01-01;0
2017-01-02;1
2017-01-11;2
2017-01-12;3
2017-02-01;4
2017-02-02;5
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfcalendar}    % <-- to convert the dates to Julian integers
\usepackage{pgfplots}
\usepackage{pgfplotstable}  % <-- to manipulate the data file/table
    \usepgfplotslibrary{dateplot}
    \pgfplotsset{compat=1.14}
    \pgfplotstableread[col sep=semicolon]{testtable.csv}\data
    % add new column with Julian integer numbers
        % therefore a counter is needed
        \newcount\julianday
    \pgfplotstablecreatecol[
        create col/assign/.code={
            % convert the number of the current row and save it to `\julianday'
            \pgfcalendardatetojulian{\thisrow{date}}{\julianday}
            % then give the entry of `\julianday' to `\entry' which is then
            % given to the current cell
            \edef\entry{\the\julianday}
            \pgfkeyslet{/pgfplots/table/create col/next content}\entry
        },
    ]{JulianDay}{\data}
    % because the `dateplot' library shifts automatically all dates to 0 using
    % the first found coordinate we can't use the created `JulianDay' data
    % directly for `linear regression', but have to do the same first with
    % the data
        % get the first coordinate of the column ...
        \pgfplotstablegetelem{0}{JulianDay}\of{\data}
        % ... and store it in `\xmin'
        \pgfmathtruncatemacro{\xmin}{\pgfplotsretval}
    % now create another column with the shifted values
    \pgfplotstablecreatecol[
        expr={\thisrow{JulianDay}-\xmin},
    ]{JulianDayMod}{\data}
\begin{document}
% --------------------------------------------------------
%% for debugging purposes only
%% show resulting numbers, if you want
%\pgfplotstabletypeset[
%    column type=l,
%    columns={date,JulianDay,JulianDayMod,value},
%    columns/date/.style={string type},
%    columns/JulianDay/.style={/pgf/number format/fixed},
%]\data
% --------------------------------------------------------
\begin{tikzpicture}
    \begin{axis}[
        date coordinates in=x,
        % =====================================================================
        % providing `xtick's works perfectly fine
        xtick={2017-01-01,2017-01-31},
        % =====================================================================
    ]
        \addplot+ [only marks] table [x=date,y=value] {\data};
        \addplot+ [mark=none]  table [
            x=date,
            % now we can use the newly created column to do the linear regression
            y={create col/linear regression={
                x=JulianDayMod,
                y=value,
            }}
        ] {\data};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.