[Tex/LaTex] how to specify colors as RGB right in the plot definitions

pgfplotstikz-pgf

Within a tikzpicture environment I want to denote several plotted curves each with a different color. If I use the syntax for each curve such as the following it works fine.

\addplot[color=olive] coordinates { 
(2, 0.0) 
(2, 0.0) 
(4, 0.0) 
(4, 0.0) 
(4, 0.0) 
(12, 0.0) 
);

However, I would like to specify the color by rgb rather than having to only select from predefined colors. Why? because my plots are machine generated, and I can compute a set of n colors for n graphs. And I'd like to avoid having to synchronize my latex file color definitions with my program which generates plot file.

I've tried several formats, all which die horrible deaths.

\addplot[color=0x89abcd] coordinates { ...}   
\addplot[color=#89abcd] coordinates { ...}   
\addplot[color=89abcd] coordinates { ...}   
\addplot[color=\color[rgb]{81,82,83}] coordinates { ...} 

Best Answer

I don't think what you're asking is possible (edit: perhaps not directly, but see percusse's answer). The pgfplots manual section 4.7.5 Colors indicates that colours have to be defined with e.g. \definecolor before use. Further, the color key belongs to TikZ, and the manual for TikZ/pgf section 15.2 Specifying a color says of /tikz/color=<color name> that

The <color name> is the name of a previously defined color.


What you could do is have your code write a series of \definecolor statements into each file, with colors named e.g. clr1, clr2 etc., and use \addplot [clr1]... etc.

Another possibility could be to define a new colormap, RGB colors can be used for that, and make a new cycle list based on that colormap. I'm not sure if this is any better though, I'm just throwing it out.

Both tikzpictures in the code below generate the same output:

output of code

\documentclass[11pt]{article} 
\usepackage{pgfplots}
\begin{document}
% Define colours inside tikzpicture environment:
\begin{tikzpicture}
\definecolor{clr1}{RGB}{81,82,83}
\definecolor{clr2}{RGB}{31,182,83}
\definecolor{clr3}{RGB}{31,18,213}
\begin{axis}[
  height=4cm,ymin=-1,ymax=3
]
\addplot [clr1, line width=5mm] coordinates { 
(2, 0.0) 
(4, 0.0) 
};

\addplot [clr2, line width=5mm] coordinates { 
(2, 1.0) 
(4, 1.0) 
};

\addplot [clr3, line width=5mm] coordinates { 
(2, 2.0) 
(4, 2.0) 
};
\end{axis}
\end{tikzpicture}

% Define a new colormap and make a cycle list based on that
\begin{tikzpicture}
\begin{axis}[
  colormap={foo}{
     % make a list of N colors
     rgb255(1)=(81,82,83);
     rgb255(2)=(31,182,83);
     rgb255(3)=(31,18,213);
  },
  % use 0,...,N-1 
  cycle list={[indices of colormap={0,...,2} of foo]},
  % the following two lines just for example
  height=4cm,ymin=-1,ymax=3,
  every axis plot/.append style={line width=5mm}
]

\addplot coordinates { 
(2, 0.0) 
(4, 0.0) 
};

\addplot coordinates { 
(2, 1.0) 
(4, 1.0) 
};

\addplot coordinates { 
(2, 2.0) 
(4, 2.0) 
};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question