我有一些长度不同的箭头。我想根据 y 坐标(第一个箭头的中点)对齐标签。我该怎么做?
代码:
\documentclass[margin=0]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-}}
\tikzset{
myarrow/.style={-{Triangle[length=1mm,width=1mm]}}
}
\tikzset{snake it/.style={decorate,decoration=snake}}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={mark=-,mark size=10mm,thick},
xtick ={0.1},
xtick style={draw=none},
xmin=-1,
xmax=0.7,
ymin=-1,
ymax=30,
axis lines=left,
only marks,
x=2cm, y=0.15cm,
]
\draw[draw=red,myarrow,thick] (-0.5,0) -- (-0.5,14.505)node[midway,above,rotate=90] {A};
\draw[draw=violet,myarrow,thick] (0.5,27.747) -- (0.5,0)node[midway,above,rotate=90] {D};
\draw[draw=red,myarrow,thick] (0.25,13.390) -- (0.25,27.747);
\draw[draw=red!75!black,myarrow,thick] (-0.25,11.306) -- (-0.25,0)node[midway,above,rotate=90] {B};
\draw[draw=red!75!black,myarrow,thick] (0,12.186) -- (0,0)node[midway,above,rotate=90] {C};
\end{axis}
\end{tikzpicture}
\end{document}
输出:
期望结果:
答案1
将路径的初始节点标记为c
,并将节点放置在 处(c-|p1)
。其中p1
是具有所需 y 值的节点。
\node (p1) at (0,6){};
\draw (-0.5,0)node(c){} -- (-0.5,14.505)node[at={(c|-p1)},above,rotate=90]{A};
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-}}
\tikzset{
myarrow/.style={-{Triangle[length=1mm,width=1mm]}}
}
\tikzset{snake it/.style={decorate,decoration=snake}}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
every axis plot post/.style={mark=-,mark size=10mm,thick},
xtick ={0.1}, xtick style={draw=none},
xmin=-1, xmax=0.7,
ymin=-1, ymax=30,
axis lines=left,
only marks,
x=2cm, y=0.15cm,
]
\node (p1) at (0,6){};
\draw[draw=red,myarrow,thick] (-0.5,0)node(c){} -- (-0.5,14.505)node[at={(c|-p1)},above,rotate=90] (){A};
\draw[draw=violet,myarrow,thick] (0.5,27.747)node(c){} -- (0.5,0)node[at={(c |- p1)},above,rotate=90] {D};
\draw[draw=red,myarrow,thick] (0.25,13.390) -- (0.25,27.747);
\draw[draw=red!75!black,myarrow,thick] (-0.25,11.306)node(c){} -- (-0.25,0)node[at={(c |- p1)},above,rotate=90] {B};
\draw[draw=red!75!black,myarrow,thick] (0,12.186)node(c){} -- (0,0)node[at={(c |- p1)},above,rotate=90] {C};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
像这样?
\documentclass[margin=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}% 1.15
\usetikzlibrary{arrows.meta}
\tikzset{arr/.style = {draw=#1, -{Triangle[length=1mm,width=1mm]}, thick},
lbl/.style = {above, rotate=90}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
every axis plot post/.style={mark=-,mark size=10mm,thick},
xtick ={0.1},
xtick style={draw=none},
xmin=-1, xmax=0.7,
ymin=-1, ymax=30,
x=2cm, y=0.15cm,
]
\draw[arr=red] (-0.5,0) -- coordinate[label={[lbl]left:A}] (aux) (-0.5,14.505);
\draw[arr=violet] (0.5,27.747) coordinate (d) -- (0.5,0);%{D};
\draw[arr=red] (0.25,13.390) -- (0.25,27.747);
\draw[arr=red!75!black] (-0.25,11.306) coordinate (b) -- (-0.25,0);%{B};
\draw[arr=red!75!black] (0,12.186) coordinate (c) -- (0,0);% {C};
% nodes
\node [lbl] at (aux -| d) {D};
\node [lbl] at (aux -| b) {B};
\node [lbl] at (aux -| c) {C};
\end{axis}
\end{tikzpicture}
\end{document}