1

I'm trying to get multi-color translated curve, I wrote a code based on a solution given here but it seems that I missed something

% arara: indent: {overwrite: yes}
% arara: pdflatex: { shell : yes }
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[scale=1.1,>=stealth]
    \colorlet{redhsb}[hsb]{red}%
    \colorlet{bluehsb}[hsb]{blue}%
    \begin{axis}[
            width=10cm,
            height=10cm,
            scale=12/9.5,
            disabledatascaling,
            axis lines =center,
            xmin= -6, xmax= 6, 
            ymin=-6, ymax=6, 
            color=cyan,
            samples=100
        ]
        % Curves
        \foreach \t in {0,.2,...,5}{
            \pgfmathtruncatemacro{\rat}{\t*10}
            \colorlet{col}{bluehsb!\rat!redhsb}
            \addplot[thin,col,smooth, domain=-2.0:2.0] {x*x*x-2*x-3+\t};
        }
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Your code can not be compiled. Error: col is not known .. – Zarko Oct 29 '15 at 20:10
  • That's what I'm missing, col is already defined \colorlet{col}{bluehsb!\rat!redhsb} but seems that there exist a conflict with addplot function here. – Hamed Oct 29 '15 at 20:16
  • I'm afraid that what you wish is not possible, i.e. you need to define explicit color list before \addplot. On this way I obtain colored curves. Maybe I'm wrong. It seem that your questions is a feature request for pgfplots author. – Zarko Oct 29 '15 at 22:03
4

A year ago I tried to achieve something similar and ended up with the following:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{xparse}

% Calculate tint for values between [#1, #3] and store result in macro \tint
% Syntax: \calctint[#1]{#2}{#3}[#4][#5]
%   #1: lower bound (default 1)
%   #2: value at which tint should be computed
%   #3: upper bound
%   #4: lower tint value (default 0)
%   #5: upper tint value (default 100)
\NewDocumentCommand\calctint{O{1}mmO{0}O{100}}
  {\pgfmathsetmacro{\tint}{#5-(#4-#5)*(#1-#2)/(#3-#1)}}

\begin{document}
\begin{tikzpicture}
\colorlet{redhsb}[hsb]{red}%
\colorlet{bluehsb}[hsb]{blue}%
  \begin{axis}
    \foreach \t in {0,0.2,...,5}{
      \calctint[0]{\t}{5}
      \edef\tmp{\noexpand
        \addplot[bluehsb!\tint!redhsb, smooth, domain=-2.0:2.0] {x*x*x-2*x-3+\t};
      }\tmp
    }
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||
  • Great! That's exactly what I'm looking for. Thanks a lot. – Hamed Oct 31 '15 at 9:32
1

As far as I understand pgfplots, you need to define cycle list before `addplot, something like this:

\documentclass[border=3mm]{standalone}
    \usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=newest}


\begin{document}
\begin{tikzpicture}[scale=1.1,>=stealth]
\begin{axis}[cycle list={
    red!00!blue,red!05!blue,red!10!blue,red!15!blue,red!20!blue,
    red!25!blue,red!30!blue,red!45!blue,red!50!blue,red!55!blue,
    red!60!blue,red!65!blue,red!70!blue,red!75!blue,red!80!blue,
    red!85!blue,red!90!blue,red!95!blue,red!100!blue,
                        },
width=10cm,
height=10cm,
axis lines=center,
xmin= -6, xmax= 6,
ymin=-6, ymax=6,
color=cyan,
domain=-2.0:2.0, samples=50, smooth,
                ]
        % Curves
\foreach \i in {0,0.2,...,5}
    \addplot {x*x*x-2*x-3+\i};
    \end{axis}
\end{tikzpicture}
\end{document}

In this case addplot cyclically takes curves color. If you define less possible colors as are curves, than color are repeated. With above code I obtain the following plot:

enter image description here

I slightly simplified the code, but this is not essential for your case. I'm couriers to, if exist a way for populate list in some loop or define colors in the loop as you (unsuccessful) try to achieve.

|improve this answer|||||
  • Thank you for your answer! But it wouldn't be practical if you want to visualise different spectrum to choose the best one or when you have to get more smoth set of translated curves. – Hamed Oct 30 '15 at 4:27
  • 1
    @Hamed, you can prepare more cyclic maps and select/combine them as you wish. Details are described in pgfplots manual in section 4.7.7 Cycle lists on pages 175 -- 184. Unfortunately, it seems that your approach doesn't work. Maybe this can be considered as bug. I'm not sure. Anyway, temporally (till to better answer) my suggestion is useful solution. – Zarko Oct 30 '15 at 5:10
  • Your upgrade did not really give a solution to my question since as you can see each curve has not a unique color ( The bottom start of the curve is red and the top end is blue). Your first answer seems to be more correct. However one can still get beautiful colored curves with your upgraded solution (replacing the line colormap = {mymap}{color=(blue!5!red) color=(blue!95!red)}, by colormap/jet, should do this) – Hamed Oct 30 '15 at 12:30
  • @Hamed, you have right. The color of curves in colormap is function of distance from minimal value of curve. I will erase this part of answer. Anyway, you meantime receive answer, which seem solve your problem. Happy TeXing! – Zarko Oct 30 '15 at 14:01

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.