1

I have a csv file with 2 "columns" separated by comma. So the .csv file looks something like

0.123,0.3232
1.244,1.325

The above numbers are the "y-values" at x=0 and x=1 respectively, i.e. the first row are the y-values of two different functions at x=0 and the second row are the y-values of the same functions but at x=1 and so on. The real csv file is 8 columns and contains thousands of x points.

My question is how can I plot the above functions in the same plot (different colors on the lines)?

My ME is

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat = newest}
\pgfplotstableread[col sep = comma]{kDist.csv}\mydata
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend pos = south east,
    xmin = 0, xmax = 1000,
    ymin = 0, ymax = 200,
    ]
    \addplot table[x index = {0}, y index = {1}]{\mydata};
    \legend{Actual}
  \end{axis}
\end{tikzpicture}
\end{document}

where kDist.csv is my csv file with all the data in it. The ME gives a totally wrong result with a single (closed) line instead of 8 separate ones.

2 Answers 2

3

You can use x expr again:

\addplot table[x expr=\coordindex,y index=0]{\mydata};
\addplot table[x expr=\coordindex,y index=1]{\mydata};

Code:

\documentclass{standalone}
\usepackage{pgfplotstable,filecontents}
\pgfplotsset{compat = 1.12}
\pgfplotstableread[col sep = comma]{%
    0.123,0.3232
    1.244,1.325
}\mydata

    \begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend pos = south east,
    xmin = 0, xmax = 2,
    ymin = 0, ymax = 2,
    ]
    \addplot table[x expr=\coordindex,y index=0]{\mydata};
    \addplot table[x expr=\coordindex,y index=1]{\mydata};
    \legend{Actual,ficticious}
  \end{axis}
\end{tikzpicture}
    \end{document}

enter image description here


If you have many columns, then \pgfplotsinvokeforeach may be better.

\documentclass{standalone}
\usepackage{pgfplotstable,filecontents}
\pgfplotsset{compat = 1.12}
\pgfplotstableread[col sep = comma]{%
    0.123,0.3232, 0.36, 0.58, 0.45, 0.21, 0.56
    1.244,1.325,  0.75, 0.85, 0.58, 0.78, 0.98
}\mydata

    \begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend pos = south east,
    xmin = 0, xmax = 2,
    ymin = 0, ymax = 2,
    ]
    \pgfplotsinvokeforeach{0,...,6}{
            \addplot table[x expr=\coordindex,y index=#1]{\mydata};}
    \legend{Actual,ficticious}
  \end{axis}
\end{tikzpicture}
    \end{document}

enter image description here

5
  • Can one add a loop to generalize it to C columns and R rows? Probably a foreach loop or something similar. Oct 14, 2015 at 11:17
  • It would also be nice to import data from a .csv or .txt file that contains 8 comma-separated columns. I try but get the error: Package PGF Math Error: Could not parse input '0.00163572307767 8211,0.001635845761509623,0.0016358684817474232,0.0016358764339052042,0.0016358 801146313213,0.0016358821140415722,0.0016358833196240732,0.0016358841020939604' as a floating point number, sorry. The unreadable part was near ',0.0016358457 61509623,0.0016358684817474232,0.0016358764339052042,0.0016358801146313213,0.00 16358821140415722,0.0016358833196240732,0.0016358841020939604'.. Oct 14, 2015 at 11:30
  • Meh, I found the easiest route was going back to M9 and export the table as a .tsv. I'll never go txt or csv again! Oct 14, 2015 at 11:35
  • @Thankyouforyourcooperation: See the update for the loop as requested.
    – user11232
    Oct 14, 2015 at 12:10
  • 2
    @Thankyouforyourcooperation For importing from a csv file, try \pgfplotstableread[col sep = comma]{myfile.csv}\mydata. These details can be found in pgfplotstable manual.
    – user11232
    Oct 14, 2015 at 12:19
0

Your MWe work as expected ... you just set maximum walue so big, that your diagram almost collapse in one point :-). Try:

\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,filecontents}
\pgfplotsset{compat = newest}
\pgfplotstableread[col sep = comma]{%
    0.123,0.3232
    1.244,1.325
}\mydata

    \begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend pos = south east,
    xmin = 0, xmax = 2,
    ymin = 0, ymax = 2,
    ]
    \addplot table[x index = 0, y index = 1]{\mydata};
    \legend{Actual}
  \end{axis}
\end{tikzpicture}
    \end{document}

enter image description here

4
  • OK to be clear and for simplicity I speak of x and y-axes: at x=0 one of the point (say blue) should be at 0.123 and the other (green) at 0.3232. Similarly at x=1 blue =1.244 and green = 1.325. When I run your code I get two points only instead of four. Oct 14, 2015 at 11:08
  • I don't understand you :-(, in table you define two points, which are placed at x_1=0.123, y_1=0.3232 and x_2=1.244, y_2=1.325. I guessing, that maybe you like to have xtick and ytick for this points?
    – Zarko
    Oct 14, 2015 at 11:14
  • Suppose I had 8 columns: All of which are y-values at x=0,1,2,....1000 and so on. Oct 14, 2015 at 11:15
  • OK, I didn't understand your question ... anyway, I correct your MWE so the others have less to do in preparation of their answers :-)
    – Zarko
    Oct 14, 2015 at 11:19

You must log in to answer this question.

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