5

Possible Duplicate:
Use first row of a table as legend entry in pgfplot graph ?

I am creating a number of very similar plots using pgfplots. The raw data for these is provided in a single .csv file, so something like

x,a,b,c,d,e
0,0.04,0.03,0.04,0.03,0.04
1,0.06,0.05,0.06,0.05,0.06
2,0.06,0.07,0.06,0.07,0.06
3,0.09,0.08,0.09,0.08,0.09
4,0.13,0.12,0.13,0.12,0.13
5,0.13,0.12,0.13,0.12,0.13

(In the real case, there are a lot more rows and columns.) Each plot is separate (i.e. not on the same axes), but they are produced 'semi-automatically' using a suitable macro containing a plot environment of the form

\newcommand{\buildplot}[1]%
  {%
    \begin{tikzpicture}
      \begin{axis}
        [
          axis x line = bottom,
          axis y line = left,
        ]
        \addplot [mark = none]
          table [col sep = comma, y index = #1] 
          {example.csv};
       \end{axis}
     \end{tikzpicture}
  }

The plots need to be differentiated, and the easiest way is to use the column header (in the example data a, b, etc.) as the title key. However, I cannot find a way to recover the header within the plotting environment to achieve this. As the information is already in the .csv, I would rather avoid retyping it in the document if at all possible.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Note that in the real case there is quite a bit more to the \buildplot macro, as there are various other settings which have to be adjusted on a plot-by-plot basis, plus some general set up to make things look good. – Joseph Wright Jan 04 '12 at 08:58
  • Not really an answer, but the datatool package (Nicola Talbot) uses provides an API for creating tabulars from CSV input. It seems to be capable of extracting the information in the first line/as well as ignoring it. –  Jan 04 '12 at 09:12
  • Perhaps the accepted answer to this question could be adapted. I recently did something similar to the requirements of this question using it without changes, saving the extracted text in a macro. – qubyte Jan 04 '12 at 09:31
  • @MarcvanDongen The {input} tag seems to adhere to the answer you provided, not to the question, and so I removed it. See http://meta.tex.stackexchange.com/questions/1931/whats-the-policy-on-retagging-questions-based-on-answers. – lockstep Jan 04 '12 at 10:51
  • You can use the approach described in http://tex.stackexchange.com/questions/23993/use-first-row-of-a-table-as-legend-entry-in-pgfplot-graph to extract entries from a table/file. – Jake Jan 04 '12 at 13:06
  • @lockstep I didn't add the tag because my solution used input but because I felt that the general solution required dealing with input. Even if pgfplots has a solution for Jospephs problem, it'd be nice to know how to deal with the ``input''. –  Jan 04 '12 at 19:34
  • @MarcvanDongen: If you think that the general solution requires dealing with the \input command, then you should add the [tag:input] tag again. – lockstep Jan 04 '12 at 19:37
  • @MarcvanDongen I've removed the [tag:input] tag here. As lockstep has said, it is not appropriate here (the question has nothing to do with \input). – Joseph Wright Jan 04 '12 at 20:07
  • @joseph It has to do with dealing with ``data input''. Perhaps we should change the tag description/add a new tag? –  Jan 04 '12 at 20:09
  • @MarcvanDongen I'm afraid I disagree: there is no issue with the data input, it's about accessing the data using the mechanisms provided by pgfplots. If you want to pursue this, I suggest popping into the chat system. (Comments are not the best way to run a discussion.) – Joseph Wright Jan 04 '12 at 20:15
  • @joseph Yes but in a broader contex a solution without pgfplots would be desirable and would help similar questions in context with different packages. That's my point. No need to reply to this. I'll pose a question in a different thread. –  Jan 04 '12 at 22:29

1 Answers1

1

The following is a partial solution that shows how you may approach it. The disadvantage is that it uses write18. The example uses head -1 to extract the first line from the .csv file and then another program to process the output, save it to a file and include the file. For the purpose of the example I use sed to change the delimiters. It's up to you to change this to whatever you want.

\immediate\write18{head -1 data.csv | sed -e 's/,/ delim /g' > file.tex}
\input{file.tex}