![将标签移近 tikz 图中的边缘](https://linux22.com/image/437969/%E5%B0%86%E6%A0%87%E7%AD%BE%E7%A7%BB%E8%BF%91%20tikz%20%E5%9B%BE%E4%B8%AD%E7%9A%84%E8%BE%B9%E7%BC%98.png)
在下图中,如何将“y”和“n”标签移近相应的边缘?我可以用来pos=...
沿边缘移动标签,但我没有找到将标签移向边缘的方法。
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,quotes}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
\graph[tree layout]{
node1[as=$x_{2}\leq-0.1254968$] -> {
node2[>"y",>swap,as=$x_{1}\leq-1.591232$] -> {
node3[>"y",>swap,as=$x_{2}\leq-0.2210189$] -> {
leaf1[>"y",>swap,draw,as=0.20 0.79 0.00],
leaf2[>"n",draw,as=0.58 0.16 0.26]
},
leaf3[>"n",draw,as=0.34 0.64 0.02]
},
leaf4[>"n",draw,as=0.20 0.70 0.10]
}
};
\end{tikzpicture}
\end{document}
答案1
修改inner sep
这些节点的 是一种方法。可以按节点进行修改,例如,node2[>"y"{inner sep=0},>swap,as=$x_{1}\leq-1.591232$]
但看起来您想为所有边缘标签更改它,因此添加会更快
edge quotes={auto, inner sep=0}
到 的选项\graph
。
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,quotes}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
\graph[tree layout,
edge quotes={auto, inner sep=0}
]{
node1[as=$x_{2}\leq-0.1254968$] -> {
node2[>"y",>swap,as=$x_{1}\leq-1.591232$] -> {
node3[>"y",>swap,as=$x_{2}\leq-0.2210189$] -> {
leaf1[>"y",>swap,draw,as=0.20 0.79 0.00],
leaf2[>"n",draw,as=0.58 0.16 0.26]
},
leaf3[>"n",draw,as=0.34 0.64 0.02]
},
leaf4[>"n",draw,as=0.20 0.70 0.10]
}
};
\end{tikzpicture}
\end{document}