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

date            value1
2010-01-01      2
2010-01-02      10
2010-01-03      8
2010-01-04      15

% formats row \ticknum of the input table using '#3'
% #1: table name
% #2: column name
% #3: formatting instruction like \day.\month.\year
    \countdef\result=\count200 %


x tick label style={yshift=-3pt,xshift=-4pt,rotate=-30,anchor=west},
\addplot table [x expr=\coordindex,y=value1] {data.dat};
\addplot table [x expr=\coordindex, y={create col/linear regression={y=value1}}] {data.dat};


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


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?


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>)
\usepackage{pgfcalendar}    % <-- to convert the dates to Julian integers
\usepackage{pgfplotstable}  % <-- to manipulate the data file/table
    \pgfplotstableread[col sep=semicolon]{testtable.csv}\data
    % add new column with Julian integer numbers
        % therefore a counter is needed
        create col/assign/.code={
            % convert the number of the current row and save it to `\julianday'
            % then give the entry of `\julianday' to `\entry' which is then
            % given to the current cell
            \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    % 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 ...
        % ... and store it in `\xmin'
    % now create another column with the shifted values
% --------------------------------------------------------
%% for debugging purposes only
%% show resulting numbers, if you want
%    column type=l,
%    columns={date,JulianDay,JulianDayMod,value},
%    columns/date/.style={string type},
%    columns/JulianDay/.style={/pgf/number format/fixed},
% --------------------------------------------------------
        date coordinates in=x,
        % =====================================================================
        % providing `xtick's works perfectly fine
        % =====================================================================
        \addplot+ [only marks] table [x=date,y=value] {\data};
        \addplot+ [mark=none]  table [
            % now we can use the newly created column to do the linear regression
            y={create col/linear regression={
        ] {\data};

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.