4

I want to plot funtions and data with own colors using \definecolor{<name>}{<model>}{<value(s)>}. If I use this command in \tikzpicture it works. But if I use this command in pgfplots all defined colors are black. Why?

My Example (not wondering about some calculations - I need that for more complex functions and data plotting):

\documentclass[rgb]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{pgffor}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{sistyle}

\newcommand{\thetamin}{-90}
\newcommand{\thetamax}{90}
\newcommand{\thetaminplusstep}{-80}

%======================================================================================

\begin{document}

\begin{tikzpicture}
\begin{axis}[title={Title},title style={text width=8cm,font=\tiny},ylabel={y}, xlabel={x}, legend style={cells={anchor=west}, legend pos=north west, font=\tiny}, xmin=-1, xmax=1, ymin=-1, ymax=1]
        \foreach \i in {\thetamin,\thetaminplusstep,...,\thetamax}{
        \newcount\k
        \pgfmathsetmacro{\k}{\i/\thetamax}
        \newcount\j
        \j=400
        \ifnum\i<0
        \pgfmathsetmacro{\j}{-400*\i/\thetamin+800} 
        \else
        \pgfmathsetmacro{\j}{(-400/\thetamax*\i)+800}
        \fi     
        \definecolor{tmpcolor}{wave}{\j}
    \colorlet{mycolor}[rgb]{tmpcolor}
        \addplot[color=mycolor, mark=none, domain=-1:1,samples=100]{\k*x};
        %\addlegendentry{$\ang{\i}$};
        }   
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\foreach \i in {\thetamin,\thetaminplusstep,...,\thetamax}{
        \newcount\k
        \pgfmathsetmacro{\k}{\i/\thetamax*2}
        \newcount\j
        \j=400
        \ifnum\i<0
        \pgfmathsetmacro{\j}{-400*\i/\thetamin+800} 
        \else
        \pgfmathsetmacro{\j}{(-400/\thetamax*\i)+800} 
        \fi     
        \definecolor{tmpcolor}{wave}{\j}
    \colorlet{mycolor}[rgb]{tmpcolor}
        \draw[fill=mycolor] (0,\k) rectangle (1,\k+0.25);
        \draw[black] (2.5,\k+0.15) node {\tiny $\ang{\i}$, wave: \j};
        }
\end{tikzpicture}

\end{document}

As result I get: define color in pgfplots

I look forward to a useful answer.

anne

  • Have you looked at the color maps section of the pgfplots manual? It is relatively easy to define a new color map yourself and then use that in a plot. – Alexander Jun 8 '16 at 11:07
  • By the way, a completely unrelated comment: You're plotting straight lines with 100 samples each. You can save some computation time if you set samples=2 instead, and it won't affect the result. – Torbjørn T. Jun 9 '16 at 12:51
1

the problem caused in \foreach, also look at \foreach not behaving in axis environment

to solve the problem I use (during using colormaps)

\edef\temp{\noexpand\addplot[color of colormap={\l}, solid, mark=none,domain=-1:1,samples=100]{\k*x};}
        \temp   

instead of

\addplot[color of colormap={\l}, solid, mark=none,domain=-1:1,samples=100]{\k*x};

result, problem solved

colorproblem-solved

also it should works without colormaps but with \definecolor?

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.