# create several lines using colormap

I'd like to create an image in latex like the one below I created in matlab (matlab2tikz is not able to transform my original figure into tex code).

I just need to know how to create a line that changes its color according to some z value. I have the (x,y) coordinates + the z value for each point. Lets supose the first point is (55,35,70), then I need a line from the origin (0,0) to the point (55,35) that changes its color (colormap/jet)

\documentclass[12pt,a4paper,twoside,openright,numbers=noenddot,chapterprefix=true]{scrbook}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{colormap/jet}
\begin{tikzpicture}
\begin{axis}[%
width=4.521in,
height=3.566in,
at={(0.758in,0.481in)},
scale only axis,
colormap/jet,
xmin=0,
xmax=100,
ymin=0,
ymax=100,
axis background/.style={fill=white},
axis x line*=bottom,
axis y line*=left]
\addplot3 [solid, point meta=explicit, forget plot]
table[row sep=crcr]{%
0    0   0\\
72.80798 91  98\\
};
\end{axis}
\end{tikzpicture}%
\end{document}


but the line I get is a black one.... no colormap at all

• You can pass the option [point meta=explicit] to the \addplot. In pgf manual (page 186) there is a brief description on how to use it. You can also specify the colormap as an option in the axis environment. For example \begin{axis}[colormap/jet] should do the job. Feb 22, 2016 at 9:19

You should not use \addplot3 if you want to print a 2D plot. You also want to tell pgfplots from which column it can get the color data:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[colormap/jet]
table [row sep=crcr,x index=0,y index=1,meta index=2]{
0 0 0\\
72.80798 91  98\\
...
};
\end{axis}
\end{tikzpicture}
\end{document}


The option row sep=crcr only serves if you have \\ at the end of each row while mesh tells pgfplots to color the segment of a line between two points.

As far as I know you cannot only specify the initial and final points and ask pgfplots to create the color transition between them for you (i.e. the color of a segment does not change), therefore you would need to import many points for each line. This output is created using 30 points in each line:

If you have many lines, you may want to include a .png image that can be produced in Matlab and use pgfplots to draw the axis. Have a look at the manual (page 63) at this link.