
Tengo algo como esto:
\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}
Con mi data.csv
archivo luciendo así:
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
Obtengo una curva que desciende (por supuesto, ya que la nueva columna se crea en la tabla dada). Lo que quiero ahora es ordenar los valores en la tabla (con la fecha como clave) primero y luego crear la nueva columna.
¿Alguien por ahí cómo hacer esto?
Respuesta1
Puede utilizar la \pgfplotstablesort
macro del pgfplotstable
paquete.
\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}