1

I am trying to plot multiple data sources on the same plot and differentiate between them with color. Here is a minimal environment for replicating the errors I am getting.

\documentclass[a4paper,12pt,openany]{book}
  
\usepackage{xcolor}
\usepackage{pgfplots}

\definecolor{LB1}{RGB}{173,216,230}
\definecolor{LB2}{RGB}{135,206,250}
\definecolor{LB3}{RGB}{0,191,255}
\definecolor{LB4}{RGB}{30,144,255}
\definecolor{LB5}{RGB}{70,130,180}
\definecolor{LB6}{RGB}{123,104,238}
\definecolor{LB7}{RGB}{0,0,255}
\definecolor{LB8}{RGB}{75,0,130}
\definecolor{LB9}{RGB}{138,43,226}

\begin{document}

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}[scale=0.9]
    \begin{axis}[
        legend style={at={(1,1)},anchor=north west},
        xlabel={$x$},
        ylabel={$y$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
    ]

    \addplot[
        color=\color{LB1},
        mark=square,
    ]
    coordinates {(2,3)(6,5)};
    \addlegendentry{$r=0.1$}

    \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

I get many errors.

! Missing \endcsname inserted.

! Extra \endcsname.
\pgfutil@colorlet ...g \csname color@#2\endcsname 

! Argument of \XC@col@rlet has an extra }.

Runaway argument?
{tikz@color}{\@undeclaredcolor }\def \reserved@b {\@declaredcolor }\futurelet \
ETC.
! Paragraph ended before \XC@col@rlet was complete.

! Package xcolor Error: Undefined color `\protect \let \reserved@d =[\def \par

! Package xcolor Error: Undefined color `tikz@color'.

How do I fix these errors?

1
  • 3
    Try color=LB1. May 2, 2022 at 8:38

1 Answer 1

3

Henri Menke already provided the solution: use color=LB1. But if you want to define your own colors, you can also define a cycle list from where pgfplots will autolatically select the color (and marker) without having to manually fix it.

(Note: You can also take a look at https://tex.stackexchange.com/a/134368/1952 to understand differences between addplot, addplot+[...] and addplot[...].)

\documentclass[a4paper,12pt,openany]{book}
  
\usepackage{xcolor}
\usepackage{pgfplots}

\definecolor{LB1}{RGB}{173,216,230}
\definecolor{LB2}{RGB}{135,206,250}
\definecolor{LB3}{RGB}{0,191,255}
\definecolor{LB4}{RGB}{30,144,255}
\definecolor{LB5}{RGB}{70,130,180}
\definecolor{LB6}{RGB}{123,104,238}
\definecolor{LB7}{RGB}{0,0,255}
\definecolor{LB8}{RGB}{75,0,130}
\definecolor{LB9}{RGB}{138,43,226}

\pgfplotscreateplotcyclelist{mycolors}{
LB1,every mark/.append style={fill=LB1!80!black},mark=*\\
LB2,every mark/.append style={fill=LB2!80!black},mark=square*\\
LB3,every mark/.append style={fill=LB3!80!black},mark=otimes*\\
LB4,mark=star\\
LB5,every mark/.append style={fill=LB5!80!black},mark=diamond*\\
LB6,densely dashed,every mark/.append style={solid,fill=LB6!80!black},mark=*\\
LB7,densely dashed,every mark/.append style={LB7,fill=LB7!80!black},mark=square*\\
LB8,densely dashed,every mark/.append style={LB8,fill=LB8!80!black},mark=square*\\
LB9,densely dashed,every mark/.append style={solid,fill=LB9},mark=otimes*\\
}

\begin{document}

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}[scale=0.9]
    \begin{axis}[
        legend style={at={(1,1)},anchor=north west},
        xlabel={$x$},
        ylabel={$y$},
        ymajorgrids=true,
        xmajorgrids=true,
        grid style=dashed,
        cycle list name=mycolors,
    ]

    \addplot
    coordinates {(0,1)(5,1)};
    \addplot
    coordinates {(0,2)(5,2)};
    \addplot
    coordinates {(0,3)(5,3)};
    \addplot
    coordinates {(0,4)(5,4)};
    \addplot
    coordinates {(0,5)(5,5)};
    \addplot
    coordinates {(0,6)(5,6)};
    \addplot
    coordinates {(0,7)(5,7)};
    \addplot
    coordinates {(0,8)(5,8)};
    \addplot
    coordinates {(0,9)(5,9)};
    \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

2
  • Why your code dont works if I replace the lines for plotting colored segments with a loop: \foreach \i in {1,2,...,9}{% \addplot coordinates (0,\i)(5,\i);} May 2, 2022 at 9:52
  • @RaffaeleSantoro It works for me, could you provide a complete code?
    – Ignasi
    May 2, 2022 at 11:42

You must log in to answer this question.

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