3

I have a pgfplot (which works fine), but I want to automatically load the data from a file: data.dat:

  ( 0 , 0.295027 )
  ( 1 , 0.295027 )
  ( 2 , 0.25965 )
  ( 3 , 0.219873 )
  ( 4 , 0.197843 )
  ( 5 , 0.18409 )
  ( 9582 , 0.0392299 )
  ( 9583 , 0.0392299 )
  ( 9941 , 0.0392299 )
  ( 9942 , 0.0392299 )
  ( 10250 , 0.0392299 )
  ( 10987 , 0.0392299 )
  ( 10988 , 0.0392299 )
  ( 10989 , 0.0392299 )
  ( 10990 , 0.0392299 )
  ( 10991 , 0.0392299 )
  ( 10992 , 0.0392299 )
  ( 10993 , 0.0392299 )
  ( 10994 , 0.0392299 )

I've been searching around and I found posts on how to read in csv file (addplot table [x=a, y=c, col sep=comma] {data.csv};), but I can't figure out how to do it with this format (a,b):

\documentclass[varwidth]{standalone}[2011/12/21]
\usepackage{pgfplots}
\pgfplotsset{compat=1.3} 
\usepackage{etex} 
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{fancybox}
\usepackage{float}
\usepackage{setspace}

\begin{document}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
axis x line=bottom,
axis y line=left,
xlabel=Number of moves,
xmode=log,log basis x=10,
xmax = 500,
ymin = 1.3]
\addplot[dotted] coordinates {
 (1,1.7)
 (500,1.7)
 };
\addplot[solid, gray] coordinates {
 ( 0 , 3.9259 )
 ( 1 , 3.9259 )
 ( 2 , 3.52535 )
 ( 10994 , 1.62303 )
};
\end{axis}
\begin{axis}[
axis x line=bottom,
axis y line=right,
xmax = 500,
ymin = 0.01,
% ymax = 0.25,
xmode=log,log basis x=10,
% xlabel=Number of moves,
%legend columns=1,
%legend entries={$f^a(s)$;, $f(s)$;, $f(s_\text{best})$},
%legend to name=named,
ylabel=DI]
% legend pos= north west]
\addplot[dashed] coordinates {
  ( 0 , 0.295027 )
  ( 1 , 0.295027 )
  ( 2 , 0.25965 )
  ( 3 , 0.219873 )
  ( 4 , 0.197843 )
  ( 5 , 0.18409 )
  ( 9582 , 0.0392299 )
  ( 9583 , 0.0392299 )
  ( 9941 , 0.0392299 )
  ( 9942 , 0.0392299 )
  ( 10250 , 0.0392299 )
  ( 10987 , 0.0392299 )
  ( 10988 , 0.0392299 )
  ( 10989 , 0.0392299 )
  ( 10990 , 0.0392299 )
  ( 10991 , 0.0392299 )
  ( 10992 , 0.0392299 )
  ( 10993 , 0.0392299 )
  ( 10994 , 0.0392299 )
};
\end{axis}
\end{tikzpicture}
\end{document}
  • Why not change the data format to one readable by the table command ? You would essentially remove the parenthesis and replace , by \t. – Nicolas Nov 5 '15 at 22:32
  • I'm actually doing that right now, but it would have been cool if I didn't have to do that :) – dorien Nov 5 '15 at 22:42
4

You can keep the input file format and tell pgfplots to silently ignore the parenthesis characters.

This works using the ignore chars key:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12} 

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=right,
xmode=log,log basis x=10,
ylabel=DI]
\addplot[dashed] table[ignore chars={(,)},col sep=comma] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}

with file

  ( 0 , 0.295027 )
  ( 1 , 0.295027 )
  ( 2 , 0.25965 )
  ( 3 , 0.219873 )
  ( 4 , 0.197843 )
  ( 5 , 0.18409 )
  ( 9582 , 0.0392299 )
  ( 9583 , 0.0392299 )
  ( 9941 , 0.0392299 )
  ( 9942 , 0.0392299 )
  ( 10250 , 0.0392299 )
  ( 10987 , 0.0392299 )
  ( 10988 , 0.0392299 )
  ( 10989 , 0.0392299 )
  ( 10990 , 0.0392299 )
  ( 10991 , 0.0392299 )
  ( 10992 , 0.0392299 )
  ( 10993 , 0.0392299 )
  ( 10994 , 0.0392299 )

enter image description here

  • 1
    Or you can just do this. :D By the way, amazing work with the package @ChristianFeuersänger. – OSjerick Nov 7 '15 at 20:29
2

Be cause you have your coordinates formatted as PGFPlots needs them, you can try to include your data file into your \addplot command. With something like this:

Add this command definition into your preamble

\makeatletter
\newcommand*\readcoords[1]{\@@input #1 }
\makeatother

Then just try it into your plot:

\addplot[dashed] coordinates {\readcoords{data.dat}};

This is based on this answer by Martin Scharrer.

0

After reconsidering and Nicolas' remark, I don't think there is an easy way. So I converted my data to the following:

\documentclass[varwidth]{standalone}[2011/12/21]
\usepackage{pgfplots}
 \pgfplotsset{compat=1.3} 
\usepackage{etex} 
 \usepackage{amsmath}
 \usepackage{graphicx}
 \usepackage{fancybox}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{setspace}

\begin{filecontents}{test.data}
coordinates {
   0    0.295027
   1    0.295027
   2    0.25965
\end{filecontents}

\begin{document}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
axis x line=bottom,
axis y line=left,
xlabel=Number of moves,
xmode=log,log basis x=10,
xmax = 500,
ymin = 1.3]
\addplot[dotted] file {test.data};
\end{axis}
\end{tikzpicture}
\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.