1

The following code plots data from two files. How can I modify the code such that the color of addplot is chosen depending on which quadrant the first x-y pair in the data file belongs to? For example, assume that I want plots originating in the second and third quadrants to be green and magenta, respectively, then tmp1.dat and tmp2.dat should be plotted in green and magenta, respectively.

\documentclass[]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.16}

\begin{document}

\begin{filecontents}{tmp1.dat}
  x y
  -1 1
  0 0
  1 1
\end{filecontents}

\begin{filecontents}{tmp2.dat}
  x y
  -1 -1
  0 0
  1 -1
\end{filecontents}

\begin{tikzpicture}
  \begin{axis}
    \foreach \i in {1,2}
    {
      \addplot table {tmp\i.dat};
    };
  \end{axis}
\end{tikzpicture}

I think this and this may be relevant.

Angelos
  • 291

2 Answers2

1

Assuming that the following is the desired output, you can use \pgfplotscreateplotcyclelist as shown in the below MWE:

enter image description here

\documentclass[]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.16}



\pgfplotscreateplotcyclelist{mylist}{%
  draw=magenta,every mark/.append style={solid, fill=magenta},mark=*\\ %1
  draw=green,every mark/.append style={solid,fill=green}, mark=*\\%2
}

\begin{document}

\begin{filecontents}{tmp1.dat}
  x y
  -1 1
  0 0
  1 1
\end{filecontents}

\begin{filecontents}{tmp2.dat}
  x y
  -1 -1
  0 0
  1 -1
\end{filecontents}

\begin{tikzpicture}
  \begin{axis}[cycle list name=mylist]
    \foreach \i in {1,2}
    {
      \addplot table {tmp\i.dat};
    };
  \end{axis}
\end{tikzpicture}

\end{document}
leandriis
  • 62,593
  • Thank you. The output is as I specified in my example. However, this solution is limited since it requires the data files to have a known order. In my use case the order is unknown. The solution I am seeking uses the content of the data file to determine the color. – Angelos Oct 08 '19 at 21:41
0

This reads the first coordinate from the data files and colors the plots accordingly. The list of colors is stored in \LstColors and the numbering of the quadrants is, let's say, unconventional but can be changed.

\documentclass[]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.16}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval}
\def\LstColors{"green","magenta","orange","red"}    
\begin{document}

\begin{filecontents}{tmp1.dat}
  x y
  -1 1
  0 0
  1 1
\end{filecontents}

\begin{filecontents}{tmp2.dat}
  x y
  -1 -1
  0 0
  1 -1
\end{filecontents}

\begin{tikzpicture}
  \begin{axis}
    \foreach \i in {1,2}
    {\pgfplotstableread{tmp\i.dat}\datatable
      \ReadOutElement{\datatable}{0}{0}{\myx}
      \ReadOutElement{\datatable}{0}{1}{\myy}
      \pgfmathtruncatemacro{\quadrant}{2*ifthenelse(\myx<0,0,1)+ifthenelse(\myy<0,0,1)}
      \pgfmathsetmacro{\mycolor}{{\LstColors}[\quadrant]}
      \edef\temp{\noexpand\addplot[color=\mycolor] table {tmp\i.dat};} 
      \temp
    };
  \end{axis}
\end{tikzpicture}  
\end{document}

enter image description here

  • Thanks! My understanding is that each file is opened twice, by \pgfplotstableread and \addplot. Would it be more efficient to reuse \datatable; \addplot[...] table {\datatable}? I tried this but I get the error "Package pgfplots Error: Could not read table file '{x}{y}' in 'search path=.'plotstable manual for details). };". I suppose it has to do with the pointer of the filereader pointing at the tables second line. How do I fix this? Related, is there any reason why \pgfplotstablegetelem are left out of Manual for Package PGFPLOTS, version 1.16, and TikZ&PGF Manual, version 3.1? – Angelos Oct 09 '19 at 08:11
  • @Angelos Please note that plotting a table from a file and plotting a loaded table are not precisely the same, see e.g. https://tex.stackexchange.com/a/356790. This is why I decided to open the file twice is the simpler solution. (I also do not precisely understand what you are trying to do.) \pgfplotstablegetelem can be found in the manual for pgfplotstable. –  Oct 09 '19 at 09:10
  • @Angelos BTW, \edef\temp{\noexpand\addplot[color=\mycolor] table {\noexpand\datatable};} \temp does work, so I really do not know what issue you are referring to. I nevertheless advise to "open the file twice". –  Oct 09 '19 at 10:08
  • I understand. Aha, I think the reason I failed to reuse \datatable is that I omitted \noexpand. The command \noexpand is a command I have to learn more about. – Angelos Oct 10 '19 at 20:11