1

Suppose I have multiple plots of experimental data series in one axis and that I also want to plot a theoretical prediction for each series. Is there any good way to group each data/theory pair together with the same color/style automatically and have only the markers of the experimental data in the legend.

Here is a (non working) example:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data1.dat}
x   y
1   0.9
2   2.1
3   3.3
4   3.9
\end{filecontents}

\begin{filecontents}{data2.dat}
x   y
1   2.1
2   3.9
3   5.8
4   8.2
\end{filecontents}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[domain=0:4]

      %% Data and theory should have the same color automatically
      \addplot+[only marks] table {data1.dat};% Data
      \addplot+[no markers] gnuplot {x};% Theory


      %% Data and theory should have the same color automatically
      \addplot+[only marks] table {data2.dat};% Data
      \addplot+[no markers] gnuplot {2*x};% Theory

      %%Some more data/theory pairs
      %% Legend with data markers only!
      \legend{1,3}
    \end{axis}
  \end{tikzpicture}

\end{document}

Result:

output

3

It can be easily done by reordering your plots. First, all the data, then all the theory. Between data and theory you reset the pgfplots cycle list (i.e. shift by the number of theory plots) to have it step through it in the same order again.

Reordering might seem tedious at first, but this way you will evade other problems such as having to skip legend entries for which I did not find an easy and intuitive solution.

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data1.dat}
x   y
1   0.9
2   2.1
3   3.3
4   3.9
\end{filecontents}

\begin{filecontents}{data2.dat}
x   y
1   2.1
2   3.9
3   5.8
4   8.2
\end{filecontents}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[domain=0:4]
    \addplot+[only marks] table {data1.dat};% Data
    \addplot+[only marks] table {data2.dat};% Data
    \pgfplotsset{cycle list shift=-2}% reset cycle list
    \addplot+[no markers] gnuplot {x};% Theory
    \addplot+[no markers] gnuplot {2*x};% Theory
    \legend{Data 1, Data 2}
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.