pgfplots classifica os valores da entrada antes de plotar

pgfplots classifica os valores da entrada antes de plotar

Eu tenho algo assim:

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

Com meu data.csvarquivo assim:

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

Recebo uma curva que desce (é claro, já que a nova coluna é criada na tabela fornecida). O que eu quero agora é classificar primeiro os valores da tabela (com a data como chave) e depois criar a nova coluna.

Alguém aí como fazer isso?

Responder1

Você pode usar a \pgfplotstablesortmacro do pgfplotstablepacote.

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

informação relacionada