我有一张数据表,其中有一个元列用于定义颜色图。现在我想要的是线图的颜色与颜色条相对应。
参见 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}%
但我希望线条颜色与色彩图定义相匹配。
答案1
TikZ 中的A\draw
只能有一种颜色。要使线条改变颜色,您需要一个mesh
绘制线段的 1D,但需要每个点的数据。
\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}
刚才看到我没有正确理解这个问题,但是可以使用相同的方法来创建线条颜色,不是从默认值point meta=y
,而是point meta=explicit
从数据中。