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).

enter image description here

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)

    scale only axis,
    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\\

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

  • 2
    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.
    – mirkom
    Feb 22, 2016 at 9:19

1 Answer 1


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:

\addplot[mesh,point meta=explicit] 
table [row sep=crcr,x index=0,y index=1,meta index=2]{
0 0 0\\
72.80798 91  98\\

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: point meta usage in pgfplots

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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