2

I would like to plot data from a txt file using pgfplots. My code for that is

\pgfplotstableread{data/Full_data_matrix.txt}{\data}
\begin{figure}[htpb]
    \centering
    \begin{tikzpicture}[scale=0.7]
    \begin{axis}[minor tick num=1,
    xlabel=Time,
    ylabel=Carrier density [$1/\si{\meter\cubed}$]
    \addplot [black,very thick] table [x={Time}, y={A}] {\data};
    \addplot [red,very thick] table [x={Time}, y={B}] {\data};
    \addplot [green,very thick] table [x={Time}, y={C}] {\data};
    \end{axis}
    \end{tikzpicture}
    \caption{Carrier concentration created by pulses at different wavelengths}
\end{figure}

Now I have twice as many points for Time, A and B as for C, but I know that each point of C corresponds to every second point in time. Is there an easy way to tell pgfplots this without having to introduce a new time scale for C?

An example for the data file is

Time  A  B  C
0  1  1  1
1  2  2  3
2  3  3  5
3  4  4  7
4  5  5  0 
5  6  6  0 
6  7  7  0

Here I would expect all lines to be exactly the same, but due to a smaller amount of data points I have to stretch C somehow.

  • Could you add the Full_data_matrix.txt file (or at least the top part which is representative) to the code, so we can have a look how it currently looks like? – Stefan Pinnow Jun 5 '18 at 15:47
  • \addplot [green,very thick] table [x expr=\coordindex, y={C}] {\data};? – Torbjørn T. Jun 5 '18 at 15:47
  • @TorbjørnT.: If my time scale is significantly different, this will plot the graphs at different positions in the figure (f.ex. if the time goes from -5 to -1, then the graph for C will still be plotted from 0 to 4) – arc_lupus Jun 5 '18 at 15:59
  • \coordindex-5 then. – Torbjørn T. Jun 5 '18 at 15:59
  • If using the correct values (instead of -5), that works @TorbjørnT. – arc_lupus Jun 5 '18 at 16:58
2

You can calculate an x-value based on row number or specific column values using x expr={<some calculation>} instead of x=<column>. To get the row number of the table (counting from 0) use \coordindex, to get the value of a specific column use \thisrow{<columnname>}. In the specific example you show, x expr={2*\thisrow{Time}} should work, but it should be combined with something like restrict x to domain=-1:7.4 to filter away the zeros at the end of the column. (If you replace the zeros in the data table with NaN you don't have to do that filtering.)

enter image description here

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\pgfplotsset{compat=1.3} % if you don't have a compat setting already
\begin{document}

\pgfplotstableread{
Time  A  B  C
0  1  1  1
1  2  2  3
2  3  3  5
3  4  4  7
4  5  5  0 
5  6  6  0 
6  7  7  0
}{\data}
\begin{figure}[htpb]
    \centering
    \begin{tikzpicture}
    \begin{axis}[
      small,
      minor tick num=1,
      xlabel=Time,
      ylabel={Carrier density [$1/\si{\meter\cubed}$]} % note braces around the whole ylabel
    ]

    \addplot [black,very thick] table [x={Time}, y={A}] {\data};
    \addplot [red,very thick] table [x={Time}, y={B}] {\data};
    \addplot [green,
              very thick,
              restrict x to domain=-1:7.4 % to filter away the last three rows
             ]
            table [
              x expr={2*\thisrow{Time}}, % an appropriate expression for this case 
              y={C}
              ] {\data};
    \end{axis}
    \end{tikzpicture}
    \caption{Carrier concentration created by pulses at different wavelengths}
\end{figure}
\end{document}

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.