# Color gradient for line in scatter plot with Tikz/pgfplots

I am trying to create a color gradient in a scatter plot that affects both the line and the markers that is not dependent on the x- or y-axis value. I have 5 levels that define my 5 marker locations, and I want the first marker to be blue and the last to be red, following a color gradient. I also want the line to match this color gradient.

Here is a minimum working example where I have succeeded in coloring the markers only:

    \documentclass[tikz,12pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Measure X},
ylabel={Measure Y},
scatter/classes={
a={blue},
b={blue!75!red},
c={blue!50!red},
d={blue!25!red},
e={red}},
]
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, scatter src=explicit symbolic]
table [x=x, y=y, meta=lvl] {
x   y   lvl
1.8 1.8 a
2.0 2.2 b
2.9 3.1 c
4.2 3.9 d
4.5 4.5 e
};
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, scatter src=explicit symbolic]
table [x=x, y=y, meta=lvl] {
x   y   lvl
0.2 0.7 a
1.3 2.2 b
3.5 3.0 c
3.8 4.3 d
5.8 5.8 e
};
\end{axis}
\end{tikzpicture}
\end{document}


Here is an example where I have colored the line. Unfortunately it doesn't start at the same blue and end at the same red for both lines:

    \documentclass[tikz,12pt]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Measure X},
ylabel={Measure Y},
]
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, mesh, colormap={}{color(0cm)=(blue); color(2cm)=(red);}]
table [x=x, y=y] {
x   y   lvl
1.8 1.8 a
2.0 2.2 b
2.9 3.1 c
4.2 3.9 d
4.5 4.5 e
};
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, , mesh, colormap={}{color(0cm)=(blue); color(2cm)=(red);}]
table [x=x, y=y] {
x   y   lvl
0.2 0.7 a
1.3 2.2 b
3.5 3.0 c
3.8 4.3 d
5.8 5.8 e
};
\end{axis}
\end{tikzpicture}
\end{document}


• Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Skillmon Mar 29 '18 at 19:21

It is not too surprising that this is the default behavior of pgfplots. You can cheat by using a somewhat stupid colormap for the first plot, which has a gradient zero, then a large gradient and then again gradient zero.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Measure X},
ylabel={Measure Y},
]
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, mesh,
colormap={}{color(0cm)=(blue);color(0.5cm)=(blue);
color(3.2cm)=(red);color(3.7cm)=(red);}]
table [x=x, y=y] {
x   y   lvl
1.8 1.8 a
2.0 2.2 b
2.9 3.1 c
4.2 3.9 d
4.5 4.5 e
};
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, , mesh,
colormap={}{color(0.1cm)=(blue); color(5.8cm)=(red);}]
table [x=x, y=y] {
x   y   lvl
0.2 0.7 a
1.3 2.2 b
3.5 3.0 c
3.8 4.3 d
5.8 5.8 e
};
\end{axis}
\end{tikzpicture}
\end{document}


• I agree with @marmot, why changing the color of the graph when the position of the points is so meaninful – BambOo Mar 29 '18 at 19:34
• The plot is to show that both Measure X and Measure Y vary in a similar manner when I vary the level A to E. Each line is to represent a subject in the experiment (7 in final version). The color confirms the order of levels is from A-E, and will be explained via a colorbar in the final version. I hope that clarifies why I want such a busy figure! – ShannonL Mar 29 '18 at 19:45
• @ShannonL I see. Makes sense. – marmot Mar 29 '18 at 19:50

I found a solution to my own question, posting this for others.

Rather than specifying mesh individually for each plot, I created my own color map and used point meta to get the color of each segment to be exactly the midpoint color of the two data points being joined. See below.

    \documentclass[tikz]{standalone}
\usepackage{graphicx}
\usepackage{tikz, pgfplots, pgfplotstable}
\pgfplotsset{compat=1.12, colormap={BlueToRed}{rgb255(0cm)=(0,0,255); rgb255(1cm)=(255,0,0)}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={Measure X},
ylabel={Measure Y},
point meta=explicit,
colormap name=BlueToRed,
]
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, mesh]
table [x=x, y=y, meta=lvl] {
x   y   lvl
1.8 1.8 1
2.0 2.2 2
2.9 3.1 3
4.2 3.9 4
4.5 4.5 5
};
\addplot [scatter, mark=*, mark size=3pt, line width=2pt, mesh]
table [x=x, y=y, meta=lvl] {
x   y   lvl
0.2 0.7 1
1.3 2.2 2
3.5 3.0 3
3.8 4.3 4
5.8 5.8 5
};
\end{axis}
\end{tikzpicture}
\end{document}


Thanks @marmot for putting me on the right track.