3

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?

Thanks.


Update:

Here is my poor man solution:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.11}


    \begin{document}


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

      }

    \end{tikzpicture}

    \end{document}

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.

1 Answers1

3

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.

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \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))};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

erik
  • 12,673
  • 1
    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
  • 1
    Please see this question that I posted in Nov 15: http://tex.stackexchange.com/questions/278357/tikzpicture-and-axis-do-not-align 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
  • @erik , is there a way to specify parts of the curve to be colored? i.e. from 0 to pi/2 green and so on. – riccs_0x Jan 02 '21 at 05:06
  • 1
    @riccs_0x I would use separate \addplot commands with a defined domain for each. – erik Jan 02 '21 at 17:04