1

I have something like this:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\usetikzlibrary{pgfplots.dateplot}

\pgfplotsset{compat=newest} % Allows to place the legend below plot

\pgfplotsset{general plot/.style={
        date coordinates in=x,
        date ZERO=2019-12-31,
        width=37cm,
        height=20cm,
        xlabel=x,
        ylabel=y,
        table/create on use/d/.style={create col/expr={\pgfmathaccuma + \thisrow{y}}},
    }
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[general plot]
        \addplot
        table[x=Date, y=d, col sep=space] {data.csv};
        \addlegendentry{legend}
    \end{axis}
\end{tikzpicture}

\end{document}

With my data.csv file looking like this:

Date        y
2020-03-20  1
2020-03-19  2
2020-03-18  3
2020-03-17  4
2020-03-16  5
2020-03-15  6
2020-03-14  7
2020-03-13  8
2020-03-12  9
2020-03-11  10

I get a curve that goes down (of course since the new column is created on the given table). What I want now, is to sort the values in the table (with the date as key) first and create the new column afterwards.

Anyone out there how to do this?

1 Answer 1

2

You can use the \pgfplotstablesort macro from the pgfplotstable package.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\usetikzlibrary{pgfplots.dateplot}

\pgfplotsset{compat=newest} % Allows to place the legend below plot

\pgfplotsset{general plot/.style={
        date coordinates in=x,
        date ZERO=2019-12-31,
        width=37cm,
        height=20cm,
        xlabel=x,
        ylabel=y,
        table/create on use/d/.style={create col/expr={\pgfmathaccuma + \thisrow{y}}},
    }
}

\begin{document}

\begin{tikzpicture}
    \pgfplotstableread{data.csv}{\loadedtable}%
    \pgfplotstablesort[sort key = Date, sort cmp = date <]{\sortedtable}{\loadedtable}
    \begin{axis}[general plot]
        \addplot
        table[x=Date, y=d, col sep=space] from \sortedtable;
        \addlegendentry{legend}
    \end{axis}
\end{tikzpicture}

\end{document}
2
  • Is there some option to do this without using \pgfplotstableread? In the manual it is described that this is only recommended for small inputs. And I don't need to sort completely, a reverse option would be enough Commented Mar 20, 2020 at 14:09
  • No more than pgfplots should only be used on small datasets. Commented Mar 20, 2020 at 14:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.