Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to draw a path in tikz whose local color is a function of position. Here are two examples of what I have in mind:

colored ptahs

I found the answers from Path following color gradient in TikZ very instructive. However, in the examples the used colormaps are simple gradients. I need to define a more complicated colormap. Is there a simple way to do this? Or are there even pre-defined colormaps? Or should I use pgfplots?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

pgfplots offers something on these lines. An example from the manual:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=none]
\addplot[mesh,samples=1000,domain=-4*pi:4*pi,line width=2pt] {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
2  
This color map is so much simpler than the shown in the question. –  OSjerick Dec 11 '13 at 19:42

I was able to produce the figure with the non-trivial colormap by computing the coordinates and colors externally and using the meta point information in pgfplots. Here is the code:

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

\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}

\begin{scope}[yshift=0cm]
    \draw[->, >=latex, color=black, line width=1.0pt, cap=round] (0.00, 0.00) -- (3.5, 0.00);
    \draw[->, >=latex, color=black, line width=1.0pt, cap=round] (0.00, -1.00) -- (0.0, 1.00);
    \begin{axis}[x=3cm, y=1cm, ticks=none, axis lines=none, colormap/jet, anchor=origin]
        \addplot[mesh, ultra thick, point meta=\thisrow{c}, shader=interp] table[x=t, y=x] {sine_wave_high_res_01.dat};
    \end{axis}
\end{scope}


\begin{scope}[xshift=5.0cm, yshift=0cm]
    \draw[->, >=latex, color=black, line width=1.0pt, cap=round] (-1.10, 0.00) -- (1.25, 0.00);
    \draw[->, >=latex, color=black, line width=1.0pt, cap=round] (0.00, -1.10) -- (0.0, 1.10);
    \begin{axis}[x=0.2cm, y=0.2cm, ticks=none, axis lines=none, colormap/jet, anchor=origin]
        \addplot[mesh, ultra thick, point meta=\thisrow{c}, shader=interp] table[x=x, y=y] {spiral_high_res_01.dat};
    \end{axis}
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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