我想将节点放置在尽可能靠近轴限值但仍在轴内的直线上,而不管节点文本的长度如何。虽然我能够使用 为首先达到最大 x 值的标签执行此操作restrict domain
,但对于首先达到最大 y 值的标签则不起作用。原因是锚点north east
先于锚点到达它south east
。但是,如果更改锚点,则文本会移到线的另一侧,而我不希望出现这种情况。
有没有办法做到这一点,而不用pos=x
像我对“斜率 6”所做的那样手动定位节点?计算 x 是可能的,但如何将节点的高度转换为轴坐标?
非常感谢。
\documentclass[11pt,a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[inner sep=1.5pt,scale=1.4]
\begin{axis}[xmin=3.0, xmax=8.0, ymin=10.0, ymax=35.0,
restrict x to domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax},
restrict y to domain=\pgfkeysvalueof{/pgfplots/ymin}:\pgfkeysvalueof{/pgfplots/ymax},samples=200]
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{3*x} node [at end,draw,anchor=south east,sloped] () {\scriptsize Slope 3};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{4*x} node [at end,draw,anchor=south east,sloped] () {\scriptsize Slope 4};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{5*x} node [at end,draw,anchor=south east,sloped] () {\scriptsize Slope 5};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{6*x} node [pos=.9,draw,anchor=south east,sloped] () {\scriptsize Slope 6};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{7*x} node [at end,draw,anchor=south east,sloped] () {\scriptsize Slope 7};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您可以用calc
它来计算位置。
\documentclass[11pt,a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[inner sep=1.5pt,box/.style={draw,font=\scriptsize}]
\begin{axis}[xmin=3.0, xmax=8.0, ymin=10.0, ymax=35.0,,scale=1.4,
restrict x to domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax},
restrict y to domain=\pgfkeysvalueof{/pgfplots/ymin}:\pgfkeysvalueof{/pgfplots/ymax},samples=200]
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{3*x}
node [at end,box,anchor=south east,sloped] (s3) {Slope 3};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{4*x}
node [at end,box,anchor=south east,sloped] (s4) {Slope 4};
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{5*x}
coordinate[pos=0] (p50) coordinate[pos=1] (p51);
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{6*x}
coordinate[pos=0] (p60) coordinate[pos=1] (p61);
\addplot[domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}]{7*x}
coordinate[pos=0] (p70) coordinate[pos=1] (p71);
\end{axis}
\path (current axis.north west) coordinate (NW)
(current axis.north east) coordinate (NE)
let \p1=($(s3.north)-(s3.south)$),
\p5=($(p51)-(p50)$),\n5={atan2(\y5,\x5)},
\p6=($(p61)-(p60)$),\n6={atan2(\y6,\x6)},
\p7=($(p71)-(p70)$),\n7={atan2(\y7,\x7)}
in
foreach \X in {5,6,7}
{($(intersection of p\X0--p\X1 and NW--NE)+(\n\X+180:{(\y1+1pt)/tan(\n\X)})$)
node[box,anchor=south east,rotate=\n\X]{Slope \X}}
;
\end{tikzpicture}
\end{figure}
\end{document}
请注意,如果您真的想要使用scale
,请在 的选项中使用它axis
,而不是在 环境中tikzpicture
。在大多数情况下,我们并不需要它,我们可以调整轴的width
和,但我把它保留了下来。height