This question already has an answer here:

I want to "draw[]" a color line starting in green, ending in blue and a transition between green and blue in a smooth continuous fashion. For example in tikz/pgf plots we can use a command such as:

\shade[bottom color=gray, top color=green] (D)--(O)--(B);

to color a triangle with gray and green with a smooth (gradient) transition.

A more specific example is shown in this figure generated with gnuplot, but I do not want to plot a function, and I do not want axes. I just want to draw[] a line or an arc. In other words, I want to "shade[]" a curve in a rainbow coloring.

smooth color on curve

This is not a repeated question: I have seen these posts with similar questions: coloring edge segments with different colors

and also: plotting a curve with different colors

That is not what I want.

Is there a function in TikZ or PGS (no pstricks or metapost please) that do this?



Here is my poor man solution:



      \coordinate (O) at (0,0);
      \foreach \t in {0.,0.01,...,1}
      \coordinate (A) at (\t, \t);
      \draw[color=blue!\s!green]  (A) circle (0.1mm);




and here the plot:

enter image description here

The main problem here is that the curve is hard coded. So, for another curve, I would need a new code.

I will work on creating a macro for a general path. Any ideas? Thanks.

marked as duplicate by OSjerick, Kurt, user13907, Werner, Paul Gaborit tikz-pgf Nov 21 '15 at 7:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Section 4.7.6 of the pgfplots documentation shows how to define your own colormap, but you need to take slightly different approaches to apply the map to symbols or lines.

    \begin{axis}[colormap={greenblue}{rgb255(0cm)=(0,0,255); rgb255(1cm)=(0,255,0)}]
    \addplot+[scatter,scatter src=x,samples=50]{sin(deg(x))};
    \addplot[mesh,ultra thick,point meta=x,samples=50]{cos(deg(x))};

enter image description here

  • This is good with pgfplots but I need to draw a segment form (A) to (B). I am uneasy about the \begin{axis} ... \end{axis} environment because I have not been able to align the axis outside of this scope. I believe I can remove the axis, but integrating this "axis" environment with TiKz has been hard for me. Thanks – Herman Jaramillo Nov 20 '15 at 23:34
  • Please see this question that I posted in Nov 15: tex.stackexchange.com/questions/278357/… Since, at the moment, I have not been able to align tikzpicture[] axis with axis[] "axis" I prefer to stay away from the "axis[]" environment. Thanks. – Herman Jaramillo Nov 20 '15 at 23:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.