2

I found yesterday this beautiful simple code:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pagestyle{empty}
\begin{document}

%\pgfplotstabletypeset{data.dat}

\vspace{1cm}

\begin{tikzpicture}
\begin{axis}[
  xlabel=Q Series,
  ylabel=P Values]
\addplot table [y=P, x=$Q_A$]{data.dat};
\addlegendentry{$Q_A$ series}
\addplot table [y=P, x=$Q_B$]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot table [y=P, x=$Q_D$]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}

\end{document}

which works well with the external file

P $Q_A$ $Q_B$ $Q_D$
10 5 7 12
8 8 10 18
6 12 16 28

Yet, if I have more comlicated numbers in the file, for example

P $Q_A$ $Q_B$ $Q_D$
10 14.2*10^2 7 12
8 8 10 18
6 12 16 28

the code crashes.

How do I have to adapt the code or data to work?

Thx in advance

3
  • I think the purpose of reading external files is that those external files are generated by a script that can not contain any "uncalculated" numbers.
    – user156344
    Commented Apr 20, 2019 at 10:13
  • So my best bet is just writing the number in full? Speicifically 1,3*10^5 -> 1300000.
    – Paul
    Commented Apr 20, 2019 at 11:02
  • You can do that of course, but I can't be sure whether it would be the best option.
    – user156344
    Commented Apr 20, 2019 at 11:03

3 Answers 3

2

You can replace *10^ by e

P $Q_A$ $Q_B$ $Q_D$
10 14.2e2 7 12
8 8 10 18
6 12 16 28
2

You can load the data in a macro and then preprocess the data. I'm assuming here that your values are in a format the \fpeval from expl3/xfp can handle (but your example value is rather large compared to the rest, so the plot looks odd):

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots,xfp}
\pagestyle{empty}
\begin{document}

\pgfplotstableread{data.dat}{\data}
\pgfplotstablecreatecol[create col/expr={\fpeval{\thisrow{$Q_A$}}}]{$Q_A$}\data
\vspace{1cm}

\begin{tikzpicture}
\begin{axis}[
  xlabel=Q Series,
  ylabel=P Values]
\addplot table [y=P, x=$Q_A$]{\data};
\addlegendentry{$Q_A$ series}
\addplot table [y=P, x=$Q_B$]{\data};
\addlegendentry{$Q_B$ series}
\addplot table [y=P, x=$Q_D$]{\data};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}

\end{document} 

enter image description here

0

You only need to tell pgfplots that these are expressions. That is, instead of

\addplot table [y=P, x=$Q_A$]{data.dat};

you only need

\addplot table [y=P, x expr=\thisrow{$Q_A$}]{data.dat};

MWE:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
P $Q_A$ $Q_B$ $Q_D$
10 14.2*10^2 7 12
8 8 10 18
6 12 16 28
\end{filecontents*}
%\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pagestyle{empty}
\begin{document}

%\pgfplotstabletypeset{data.dat}

\vspace{1cm}

\begin{tikzpicture}
\begin{axis}[
  xlabel=Q Series,
  ylabel=P Values]
\addplot table [y=P, x expr=\thisrow{$Q_A$}]{data.dat};
\addlegendentry{$Q_A$ series}
\addplot table [y=P,  x expr=\thisrow{$Q_B$}]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot table [y=P,  x expr=\thisrow{$Q_D$}]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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