3

I have a tikzpicture with many lines of varying sizes. I decided to separate the data from the plotting logic by creating a pgftable with all my data the selecting the data rows that I want to plot. This works fine and leads to clean tikzpicture source, but it forces me to pad my data with nan values which bloat my file size and hence the table loading time and compilation time. I can of course separate my data into multiple files, but I prefer to manage a single data source.

Is this possible to do without padding my data with nan values?

MWE

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

% My file is much bigger than this so padding my data
% with nans becomes untenable. 
\usepackage{filecontents}
\begin{filecontents}{data.txt}
  X1 1 2 3 4 5 6 7 8 9 10
  Y1 1 2 3 4 5 6 7 8 9 10
  X2 1 2 nan nan nan nan nan nan nan nan
  Y2 1 1 nan nan nan nan nan nan nan nan
\end{filecontents}

\begin{document}

  \pgfplotstabletranspose[input colnames to={X1}, colnames from={X1}]{\loadedtable}{data.txt}

  \begin{tikzpicture}
    \begin{axis}
      \addplot table [x=X1, y=Y1] {\loadedtable};
      \addplot table [x=X2, y=Y2] {\loadedtable};
    \end{axis}
  \end{tikzpicture}

\end{document}
3

Just use a proper column separator like a comma. Then you can have empty cells by simply placing the commas right next to each other. You just have to tell pgfplotstable what column separator you are using via the col sep option.

With that method your MWE would look like this:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

% My file is much bigger than this so padding my data
% with nans becomes untenable. 
\usepackage{filecontents}
\begin{filecontents}{data.txt}
  X1,1,2,3,4,5,6,7,8,9,10
  Y1,1,2,3,4,5,6,7,8,9,10
  X2,1,2,,,,,,,,
  Y2,1,1,,,,,,,,
\end{filecontents}

\begin{document}
  \pgfplotstableset{col sep=comma}
  \pgfplotstabletranspose[input colnames to={X1}, colnames from={X1}]{\loadedtable}{data.txt}

  \begin{tikzpicture}
    \begin{axis}
      \addplot table [x=X1, y=Y1] {\loadedtable};
      \addplot table [x=X2, y=Y2] {\loadedtable};
    \end{axis}
  \end{tikzpicture}

\end{document}

It's not perfect as you still have to place all the commas but it's certainly better than the solution with the nan approach.

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.