# how to specify colors as RGB right in the plot definitions

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 { ...}

• Could you elaborate a bit on what you mean by "my plots are machine generated, and I can compute a set of n colors for n graphs" please. So you want to replicate already existing plots with PGFPlots and you want to use exactly the same color? – Stefan Pinnow Jan 10 '18 at 15:42
• Elaboration: I have code which generates a set of .ltxdat files which I include in my latex document via \input{filexyz.ltxdat}. I have several different latex files which input different overlapping subsets of the various .ltxdat files. Each .ltxdat file uses one \begin{tikzpicure} ...\end{tikzpicture} do define some number of plots, each file having potentially a different number of plots. Depending on the number of curves, I chose a set of colors which are "visibly different", and I set the color of each addplot accordingly. Does that help? – Jim Newton Jan 10 '18 at 16:08
• Could you have your code write \definecolor{col1}{RGB}{81,82,83} to the start of the tikzpicture and then use \addplot[color=col1] ..? Other than that, would writing options to the axis be a possibility? I think (but I'm not sure) that colours have to be named colours, but you could generate a colourmap with RGB colours, and make a cycle list based on the colourmap. – Torbjørn T. Jan 10 '18 at 16:51
• If the correct answer is that "No, it is not possible", then sigh, that's the answer. Does it hurt anything if I end up defining the same color many times and they all get included in the latex file? Or do I need to try to only define each color once? – Jim Newton Jan 10 '18 at 17:10
• If you define a colour inside a tikzpicture environment it won't be available outside it, but I don't think it hurts to have multiple definitions. – Torbjørn T. Jan 10 '18 at 17:51

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:

\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}
]

(2, 0.0)
(4, 0.0)
};

(2, 1.0)
(4, 1.0)
};

(2, 2.0)
(4, 2.0)
};
\end{axis}
\end{tikzpicture}
\end{document}


You can make up your own color setter via some keys but I think setting up a proper colormap as Torbjørn showed is a more structured solution.

\documentclass[11pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[define rgb/.code={\definecolor{mycolor}{RGB}{#1}},
rgb color/.style={define rgb={#1},mycolor}]
\begin{axis}[height=4cm,ymin=-1,ymax=3]
\addplot [rgb color={81,81,81}, line width=5mm] coordinates { (2, 0) (4, 0) };
\addplot [rgb color={81,182,83}, line width=5mm] coordinates { (2, 1) (4, 1) };
\addplot [rgb color={31,18,213}, line width=5mm] coordinates { (2, 2) (4, 2) };
\end{axis}
\end{tikzpicture}
\end{document}


• Sorry, I can't figure out how to get code formatting in a comment. Here is what I decided to do, which seems to work. Before each \addplot I insert a \definecolor with a machine generated name. \definecolor{color008941}{RGB}{0,137,65} \addplot[color=color008941] coordinates { (4, 0.0) (9, 0.0) (16, 0.0) }; \definecolor{color6}{RGB}{0,111,166} \addplot[color=color6] coordinates { (1, 0.0) (36, 0.001) }; \definecolor{color7}{RGB}{122,73,0} \addplot[color=color7] coordinates { (16, 0.0) (100, 0.003) }; – Jim Newton Jan 16 '18 at 13:15
• @JimNewton That's what I'm doing too. but only as a keyword to addplot – percusse Jan 16 '18 at 14:12