# PGFplots and table with different number of elements in columns

I am trying to plot a table where I have different number of elements in the columns. In the MWE, the pairs (x1,y1),...,(x4,y4) all have different number of elements, can I use PGFplots in such situation, ithout having to manually arrange the table or spit it in 4 different tables?

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

x1 y1  x2 y2   x3 y3  x4 y4
1  0.5 1  0.2  1  0.1 1  0.3
2  0.7 2  0.4  2  0.2 2  0.5
3  0.3 3  0.6  3  0.4 3  0.6
4  0.1 4  0.3  4  0.4
5  0.2  5  0.2
6  0.2
7  0.1
}\mytable

\pgfplotsset{%
compat=1.8,
compat/show suggested version=false,
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={x-axis},ylabel={y-axis}]
\legend{1,2,3,4}
\end{axis}
\end{tikzpicture}
\end{document}

-
Could you export the table with delimiters (, or Tab or …)? Otherwise, empty groups do help. – Qrrbrbirlbel May 17 '13 at 12:40
That table come from an external program, such as gnuplot or Matlab. The delimiter there is the space, like [x1 y1 x2 y2 x3 y3 x4 y4], so it would be better to keep it like in my question. However, other users may need with colons or tabs, therefore, it would also be useful an answer with those. – cacamailg May 17 '13 at 12:47

Unless you use a different col sep, you will need to use empty groups.

From the pgfplotstable manual, section 2.1 “Text Table Input Format”, p. 5f:

Furthermore, if you need empty cells in case col sep≠space, you have to provide {} to delimit such a cell since col sep=space uses at least one white space (consuming all following ones).

This is the usual way how TeX treats spaces.

An alternative solution would be to use a different column seperator, e.g. a comma:

\pgfplotstableread[col sep=comma]{
x1, y1,x2, y2,x3, y3,x4, y4
1,0.5, 1,0.2, 1,0.1, 1,0.3
2,0.7, 2,0.4, 2,0.2, 2,0.5
3,0.3, 3,0.6, 3,0.4, 3,0.6
4,0.1, 4,0.3, 4,0.4,  ,
,   , 5,0.2, 5,0.2,  ,
,   ,  ,   , 6,0.2,  ,
,   ,  ,   , 7,0.1,  ,
}\mytable


(The spaces here are only for a visual reference.) For simple numbers that get parsed by pgfplots anyway, the additional spaces do not hurt, but for various other use cases, the option /pgfplots/table/trim cells might be helpful.

## Code

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

x1 y1  x2 y2   x3 y3  x4 y4
1  0.5 1  0.2  1  0.1 1  0.3
2  0.7 2  0.4  2  0.2 2  0.5
3  0.3 3  0.6  3  0.4 3  0.6
4  0.1 4  0.3  4  0.4 {} {}
{} {}  5  0.2  5  0.2 {} {}
{} {}  {} {}   6  0.2 {} {}
{} {}  {} {}   7  0.1 {} {}
}\mytable

\pgfplotsset{%
compat=1.8,
compat/show suggested version=false,
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={x-axis},ylabel={y-axis}]
\legend{1,2,3,4}
\end{axis}
\end{tikzpicture}
\end{document}


## Output

-
Can you point me the page, just for future reference? Can I use the same approach in the original table that comes from the external program, I mean, without using pgfplotstable? – cacamailg May 17 '13 at 12:53
@cacamailg I’ve added the reference. I’m not so sure what you mean with your second sentence. If I use the {}-filled table instead of \mytable in one of the \addplot commands, it works too. – Qrrbrbirlbel May 17 '13 at 13:00
My second sentence was exactly that! Thanks. – cacamailg May 17 '13 at 14:02