1

I have a data file of the form

date          1    2    3    4
2019-10-28  120   40   55  100
2019-10-29  100   45   30   80
2019-10-30   90   65  100   70

i.e. with four consecutive values per date. Is there a possibility using pgfplots to plot this as one long data column by concatenating the values row-wise, such that it is plotted like

date        number    value
2019-10-28       1      120
2019-10-28       2       40
2019-10-28       3       55
2019-10-28       4      100
2019-10-29       1      100
2019-10-29       2       45
2019-10-29       3       30
2019-10-29       4       80
.
.
.

without reorganizing the data file itself?

In some way this is the inversion of plot data where x-column resets/where data is concatenated instead of stacked column wise.

1
  • Probably yes, but I do not know how you want to plot the second file. Please add an explicit example (in the form of an MWE) that illustrates how you are going to use the data of the second file.
    – user194703
    Commented Oct 31, 2019 at 22:41

1 Answer 1

1

You can always create a new table on the format you describe. I also don't know what kind of plot you're actually after, so I didn't do anything in particular with regards to that.

\documentclass{article}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
% read original file to table
\pgfplotstableread{
date          1    2    3    4
2019-10-28  120   40   55  100
2019-10-29  100   45   30   80
2019-10-30   90   65  100   70
}\dataA
% get number of rows in table
\pgfplotstablegetrowsof{\dataA}
\pgfmathtruncatemacro{\Nrows}{\pgfplotsretval*4}

% make a new table
\pgfplotstablenew[
  % with a column called number
  create on use/number/.style={
     % containing numbers given by this expression
     create col/expr={int(mod(\pgfplotstablerow,4)+1)}
  },
  columns={number}
]{\Nrows}{\dataB}

% create a new column called date
\pgfplotstablecreatecol[
 create col/assign/.code={
   % \pgfplotstablerow is row number in \dataB
   % calculate a row number for lookup in \dataA
   \pgfmathtruncatemacro{\RowNo}{int(floor(\pgfplotstablerow/4))}
   % get an entry from \dataA
   \pgfplotstablegetelem{\RowNo}{date}\of\dataA
   \edef\entry{\pgfplotsretval}
   % and write to \dataB
   \pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{date}\dataB

% and finallyb create the value column
\pgfplotstablecreatecol[
 create col/assign/.code={
   % similar to above, just need to calculate a column number as well
   \pgfmathtruncatemacro{\RowNo}{int(floor(\pgfplotstablerow/4))}
   \pgfmathtruncatemacro{\ColNo}{int(mod(\pgfplotstablerow,4)+1)}
   \pgfplotstablegetelem{\RowNo}{[index]\ColNo}\of\dataA
   \edef\entry{\pgfplotsretval}
   \pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{value}\dataB


\begin{document}
\pgfplotstabletypeset[string type]{\dataA}    
\pgfplotstabletypeset[columns/date/.style={string type}]{\dataB}

\begin{tikzpicture}
\begin{axis}[
  date coordinates in=x,
  xtick=data
]

\addplot table[x=date,y=value] {\dataB};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .