0

I have a table of data with a meta column to define the colormap. Now what I want is that the color of the lineplot corresponds to the colorbar.

See the MWE:

\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
width=8cm,
height=4cm,
xmin=-0.3, xmax=6.3,
xticklabels={,,},
colormap={reds}{rgb255=(255,255,255) color=(red)},
colorbar,
]
\addplot+[const plot mark right, point meta=explicit]
table[
    x index=2,
    y index=1,
    meta index=0] {%
0.0 0.03104373101587135 0
0.0 0.03104373101587135 1
0.0 1.3085897356901182 2
0.0 14.510839602608515 3
0.0 1.051687401397803 4
0.0 0.037145197995416115 5
0.0 0.0 6

10.0 0.06894699381520336 0
10.0 0.06894699381520336 1
10.0 2.5049705530054065 2
10.0 17.52958223184109 3
10.0 4.047396608031903 4
10.0 0.3384635297420135 5
10.0 0.0 6
};

\end{axis}
\end{tikzpicture}%
\end{document}%

What I get is:
image

But I would like to have the line colors to match the colormap definition.

1 Answer 1

0

A \draw in TikZ can only have one color. To make the lines change color, you need a 1D mesh that draws line segments, but needs data for every point.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=4cm,
xmin=-0.3, xmax=6.3,
xticklabels=\empty,
colormap={reds}{rgb255=(255,255,255) color=(red)},
colorbar,
axis background/.style={fill=gray!30}
]
\addplot+[const plot mark right, only marks] table[x index=1, y index=0] {
0.03104373101587135 0
0.03104373101587135 1
1.3085897356901182 2
14.510839602608515 3
1.051687401397803 4
0.037145197995416115 5
0.0 6

0.06894699381520336 0
0.06894699381520336 1
2.5049705530054065 2
17.52958223184109 3
4.047396608031903 4
0.3384635297420135 5
0.0 6
};
\addplot[mesh, ultra thick] table[x index=1, y index=0] {
0.03104373101587135 0
0.03104373101587135 1
1.3085897356901182 1
1.3085897356901182 2
14.510839602608515 2
14.510839602608515 3
1.051687401397803 3
1.051687401397803 4
0.037145197995416115 4
0.037145197995416115 5
0.0 5
0.0 6
};
\addplot[mesh, ultra thick] table[x index=1, y index=0] {
0.06894699381520336 0
0.06894699381520336 1
2.5049705530054065 1
2.5049705530054065 2
17.52958223184109 2
17.52958223184109 3
4.047396608031903 3
4.047396608031903 4
0.3384635297420135 4
0.3384635297420135 5
0.0 5
0.0 6
};
\end{axis}
\end{tikzpicture}
\end{document}

Gray graph with red white constant plots

Just seen that I have not understood the problem correctly, but the same method can be used to create a line color not from the default point meta=y, but point meta=explicit from the data.

You must log in to answer this question.

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