4

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}

enter image description here

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: enter image description here

1 Answer 1

4

Something like this may be:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [mesh, variable=\x, samples=100,domain=-10:10] ({\x},{sin(deg(\x))+0.6*\x});
\end{axis}
\end{tikzpicture}
\end{document}

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

enter image description here

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}]
\addplot [mesh, variable=\x, samples=100,domain=-10:10] ({\x},{sin(deg(\x))+0.6*\x});
\end{axis}

\end{tikzpicture}
\end{document}

which will give you:

enter image description here

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

enter image description here

12
  • 2
    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, 2019 at 12:32
  • 1
    clear thanks I will try to cook up something Mar 1, 2019 at 12:38
  • 1
    @sheß OOps, in that case, I will wait for some our in house TikZperts to answer your question ;) Mar 1, 2019 at 13:02
  • 1
    Done adding, lately, falcons are going well together with @marmot. Mar 1, 2019 at 15:25
  • 1
    @sheB as much as it is nice to remove them, I would prefer to keep them as it gives a chronological evolution. Mar 1, 2019 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .