灵感来自这个问题我尝试用一个末端箭头来装饰我的 PGFPlots 网格。但是,这个末端箭头的位置不正确,导致线的末端显示在箭头尖端后面。这是一个最小的工作示例。大部分内容是从主要答案上述问题。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usetikzlibrary{decorations.markings}
\def\definemappedcolor#1{%
%\message{Got #1^^J}%
\pgfmathparse{#1*1000}% ... transform to range [0,1000]
\pgfplotscolormapdefinemappedcolor{\pgfmathresult}%
}%
\tikzset{
set arrow inside/.code={\pgfqkeys{/tikz/arrow inside}{#1}},
set arrow inside={end/.initial=>, opt/.initial=},
/pgf/decoration/Mark/.style={
mark/.expanded=at position #1 with
{
\noexpand\definemappedcolor{#1}%
\noexpand\arrow[\pgfkeysvalueof{/tikz/arrow inside/opt}]{\pgfkeysvalueof{/tikz/arrow inside/end}}
}
},
arrow inside/.style 2 args={
set arrow inside={#1},
postaction={
decorate,decoration={
markings,Mark/.list={#2}
}
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[x=1cm, y=1cm, colormap/hot, point meta=explicit]
\addplot[mesh]
coordinates {
(0,0) [0]
(1,1) [1]
(2,2) [2]
(3,3) [3]
(4,2) [4]
};
\addplot[draw=none]
coordinates {
(0,0) [0]
(1,1) [1]
(2,2) [2]
(3,3) [3]
(4,2) [4]
} [arrow inside={end=stealth,opt={mapped color, scale=3}}{1}];
\end{axis}
\end{tikzpicture}
\end{document}
结果如下:
您可以清楚地看到,箭头尖端没有正确地连接到图的末尾,而是略微位于末尾之前。我尝试过使用shorten >=2pt
,但似乎不起作用。
我怎样才能定位箭头,以便它“正确地”附着在情节线的末尾,即,我怎样才能防止看到情节线的末尾?
答案1
这里的问题是,您将箭头绘制在“之前绘制的内容”之上。这就是为什么没有“自动更正”此行为的原因,因为当您直接将箭头应用到线上时就会出现这种情况。
为了“模拟”该行为,您可以绘制一个双箭头。第一个箭头只是覆盖了“其他”线中不应看到的部分,第二个箭头是您想要添加的“真实”箭头。为此,您必须使用负分隔长度。
此外,我尝试寻找一种独立于所选线宽的解决方案,并且针对线宽达到“超厚”进行了测试。
有关更多详细信息,请查看代码中的注释。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
% load this library to make use of the advanced arrow features
arrows.meta,
decorations.markings,
pgfplots.colormaps,
}
\def\definemappedcolor#1{%
%\message{Got #1^^J}%
\pgfmathparse{#1*1000}% ... transform to range [0,1000]
\pgfplotscolormapdefinemappedcolor{\pgfmathresult}%
}%
\tikzset{
set arrow inside/.code={\pgfqkeys{/tikz/arrow inside}{#1}},
set arrow inside={end/.initial=>, opt/.initial=},
/pgf/decoration/Mark/.style={
mark/.expanded=at position #1 with {
\noexpand\definemappedcolor{#1}%
\noexpand\arrow[\pgfkeysvalueof{/tikz/arrow inside/opt}]{\pgfkeysvalueof{/tikz/arrow inside/end}}
}
},
arrow inside/.style 2 args={
set arrow inside={#1},
postaction={
decorate,decoration={
markings,Mark/.list={#2}
},
},
},
% -------------------------------------------------------------------------
my arrow/.tip={
% this arrow tip shall overdraw the end of the "other" draw line
Rectangle[
% scale this arrow to 5 times the length of `line width'
% (you can adjust this; it must only be longer than the "free" seen
% stuff of the "other" line)
% and (only) being as width as `line width'
length={0pt 5},
width={0pt 1},
% now adjust the `sep' to make it independent of line width
sep={-3.05pt -5},
% assume white background color
% white,
green, % for debugging purposes only
]
% and this is the arrow you actually want to plot
Stealth[]
},
% -------------------------------------------------------------------------
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x=1cm,
y=1cm,
colormap/hot,
point meta=explicit,
]
\addplot [
mesh,
% make (already) active to prove that I didn't move the line or arrow tip
very thick,
] coordinates {
(0,0) [0]
(1,1) [1]
(2,2) [2]
(3,3) [3]
(4,2) [4]
};
\addplot [
draw=none,
% make sure, this has the same `line width' as the previous plot
% very thick,
]
coordinates {
(0,0) [0]
(1,1) [1]
(2,2) [2]
(3,3) [3]
(4,2) [4]
% apply the defined arrow here
} [arrow inside={end=my arrow}{1}];
\end{axis}
\end{tikzpicture}
\end{document}