Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to read from a file with 100000 rows (I plot only the first 600) and 12 columns of data and to plot it with different colors. The data does not contain x components, that is why I use x expr=\coordindex in my code.

At the moment I am facing two problems with my plot.

  1. The different columns have the same color (black)
  2. The label in the legend should be $d_1$,...,$d_{12}$ instead of $d_0$,...,$d_{11}$

I tried the following code:

\documentclass[pdftex]{scrbook}
\usepackage{pgfplots,pgfplotstable}

\newcommand{\plotfile}[1]{
 \pgfplotstableread{#1}{\table}
 \pgfplotstablegetcolsof{#1}
 \pgfmathtruncatemacro\numberofcols{\pgfplotsretval - 1}
 \pgfplotsinvokeforeach{0,...,\numberofcols}{
 \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
   \addplot[only marks, mark size=0.3pt,mark=*] table [x expr=\coordindex,y index=##1] {#1};
   \addlegendentryexpanded{$d_{##1}$}
 }
}

\begin{document}
 \begin{tikzpicture}
  \begin{axis}[xlabel={Influx},
    ylabel={Center of mass},
    grid=major,
    ymin=-1,
    ymax=1,
    xmin=0,
    xmax=600,      
    legend style={
      font=\footnotesize,
      at={(1.3,1)},
      anchor=north east}]
    \plotfile{./tables/schwerpunkt_12_.0220.log}
  \end{axis}
 \end{tikzpicture}
\end{document}

The result I get is:

plot

EDIT

I was able using the tip from @Jake (color list) to create my plot with 12 different colors. What is still not solved, is that i would like to add +1 to the number near d. This mean, instead of $d_0$, there should be $d_1$ and etc.

My guess is that i need to create somehow a macro that do a summation of ##1 + 1 and the result of it to place in addlegendentryexpanded{$d_{##1}$} instead of ##1

How could i do it?

share|improve this question
    
Can you please add in a little more explanation? It's not quite clear what you're attempting to do with this code. –  qubyte Feb 16 '12 at 15:23
    
You need to use \addplot+[only marks,.... If you don't use the +, the colour list is deactivated. Note, however, that the default colour list only contains four different colours, so you should try one of the other cycle list names (like cycle list name=color list or exotic). However, even then I doubt you'll be able to clearly tell the 12 series apart (12 is a lot). –  Jake Feb 16 '12 at 18:24
    
@jake, until now i was able to get only up to four colors, and then tikz start to use different shapes... what i don't want –  Eagle Feb 16 '12 at 18:36
    
Do you want line plots (then you have to set no markers), or do you want symbols (then you have to set only marks)? Have you tried other cycle lists, as I described in my previous comment? –  Jake Feb 16 '12 at 18:38
    
i thought that by writitng only marks it will not connect the points with a line, but i didnt took into consideration the symbols... need to try –  Eagle Feb 16 '12 at 18:52

1 Answer 1

up vote 4 down vote accepted

Here is a proposal. I included a small lua script to generate a data set (if you don't want to use lualatex, then remove the \directlua part and change the table file to the one containing the actual data.

To compute the sum, I use \numexpr (cheaper than another call to the pgfmath parser).

% Compile with lualatex
\documentclass{standalone}

\usepackage{pgfplots,pgfplotstable}

\directlua{%
  local f1 = function (t, tau)
    return tostring(math.exp(-t/(600*tau)))
  end
  local f2 = function (t, tau, f)
    return tostring(math.exp(-t/(600*tau))*math.cos(t*f))
  end
  local filehandle, errormessage = io.open("table_example.log","w")
  for i = 1, 600 do
      filehandle:write(
        f1(i, .05) .. ";" .. 
        f1(i, .1) .. ";" .. 
        f1(i, .2) .. ";" .. 
        f1(i, .3) .. ";" .. 
        f1(i, .4) .. ";" .. 
        f1(i, .5) .. ";" .. 
        f2(i, .05, 100) .. ";" .. 
        f2(i, .1, 100) .. ";" .. 
        f2(i, .2, 100) .. ";" .. 
        f2(i, .3, 100) .. ";" .. 
        f2(i, .4, 100) .. ";" .. 
        f2(i, .5, 100) .. "\noexpand\n")
  end
  filehandle:close()}

\begin{document}

\pgfplotsset{%
  compat = 1.5,
  cycle list name = exotic,
  table/col sep = semicolon}

\newcommand{\plotfile}[1]{%
  \pgfplotstableread{#1}{\table}
  \pgfplotstablegetcolsof{#1}
  \pgfmathtruncatemacro\numberofcols{\pgfplotsretval - 1}
  \pgfplotsinvokeforeach{0,...,\numberofcols}{%
    \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
    \addplot+[only marks, mark size=0.3pt] table [x expr=\coordindex,y
    index=##1] {#1}; 
    \addlegendentryexpanded{$d_{\number\numexpr##1+1\relax}$}
  }
}

\begin{tikzpicture}
  \begin{axis}[xlabel={Influx},
    ylabel={Center of mass},
    grid=major,
    ymin=-1,
    ymax=1,
    xmin=0,
    xmax=600,      
    legend style={
      font=\footnotesize,
      at={(1.3,1)},
      anchor=north east}]
    \plotfile{./table_example.log}
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.