# Draw one function, varying color by (gradient) using another function's value

I'm trying to draw something in TikZ where I want to vary the draw color.

E.g. I'm drawing this line

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\draw plot[variable=\x, samples=100,domain=-10:10] ({\x},{sin(deg(\x))+0.6*\x});
\end{tikzpicture}
\end{document}


But now I want the color to indicate the value of a function, e.g. c = x2.

Edit: With x2 this could then look like:

Something like this may be:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


I just changed the \draw environment with \addplot within an axis environment. This will give you:

Update Fix with colormaps. By utilising the colormaps tikz library you can set the desired color map and obtain what you desire. As in:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
% Note that this is the colormap definition. You can play with this however you want. Fun fact: this color map also includes 8bit color code definition. Which can be defined using rgb255 = {value,value,value}.
\pgfplotsset{colormap={CM}{rgb=(0,0,1) color=(red) rgb=(0,0,1)}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[point meta = y, colormap name={CM}]
\end{axis}

\end{tikzpicture}
\end{document}


which will give you:

Update Credits goes to @marmot.

You can an inline function in point meta definition to customise your colormap implicitly using an explicit function (see @marmot's comments).

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[ colormap/hot] \addplot [point meta = x*x*x,mesh, samples=100,domain=-10:10] {sin(deg(x))+0.6*x};
\end{axis}
\end{tikzpicture}
\end{document}


Here, I used cubic function for manipulating the color map. This will give

• Thanks, but I think this misses the bit that the color gradient should depict this a function, (in my question i used the example of x^2) – sheß Mar 1 at 12:32
• clear thanks I will try to cook up something – Raaja Mar 1 at 12:38
• @sheß OOps, in that case, I will wait for some our in house TikZperts to answer your question ;) – Raaja Mar 1 at 13:02
• Done adding, lately, falcons are going well together with @marmot. – Raaja Mar 1 at 15:25
• @sheB as much as it is nice to remove them, I would prefer to keep them as it gives a chronological evolution. – Raaja Mar 1 at 21:53