xmin
在下面的最小工作示例中,我使用、xmax
和键ymin
来ymax
计算注释的坐标。但是,这仅在轴选项中明确设置轴限值时才有效。
当我没有明确设置轴限值时,我也希望代码能够正常工作。在这种情况下,轴限值存储在哪些键名中?
最小工作示例:
% Mind section '4.17 Custom annotations' of the PGFplots manual Revision 1.12 (2015/01/31).
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\newcommand{\slopeTriangle}[9]
{
% Calculate auxilliary quantities.
\pgfmathsetmacro{\xA}{#6+(#1+#2)*(#7-#6)}
\pgfmathsetmacro{\yA}{#8+#3*(#9-#8)}
\pgfmathsetmacro{\xB}{#6+#1*(#7-#6)}
\pgfmathsetmacro{\yB}{\yA}
\pgfmathsetmacro{\xC}{\xA}
\pgfmathsetmacro{\yC}{\yA+(\xA-\xB)*#4}
% Define coordinates for \draw.
\coordinate (A) at (axis cs:\xA,\yA);
\coordinate (B) at (axis cs:\xB,\yB);
\coordinate (C) at (axis cs:\xC,\yC);
% Draw slope triangle.
\draw[#5] (A)--(B) node[pos=0.5,anchor=north] {1};
\draw[#5] (B)--(C);
\draw[#5] (C)--(A) node[pos=0.5,anchor=west] {1};
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},
xmin=-0.1, % WHAT IF I REMOVE THIS?
xmax=1.1, % WHAT IF I REMOVE THIS?
xlabel=$x$,
ytick={-0.2,0,2,2.2},
ymin=-0.2, % WHAT IF I REMOVE THIS?
ymax=2.2, % WHAT IF I REMOVE THIS?
ylabel style={rotate=-90},
ylabel=$y$,
unit vector ratio=2 1 1,
clip=false
]
\addplot[blue,domain=0:1] {x};
\addplot[red,domain=0:1] {2*x};
\pgfkeysgetvalue{/pgfplots/xmin}{\xmin} % IF THE xmin IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
\pgfkeysgetvalue{/pgfplots/xmax}{\xmax} % IF THE xmax IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
\pgfkeysgetvalue{/pgfplots/ymin}{\ymin} % IF THE ymin IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
\pgfkeysgetvalue{/pgfplots/ymax}{\ymax} % IF THE ymax IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
\slopeTriangle{0.8}{0.1}{0.1}{1}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
根据pgfplots
的参考文献/pgfplots/xmin
,它成立
在可视化阶段,即在 \end{axis} 期间,这些键将设置为最终轴限值。您可以通过以下方式访问这些值
\pgfkeysvalueof{/pgfplots/xmin}
因此,您只需将您的计算和节点生成括起来\pgfplotsextra
,您的代码就会收到预期的(自动计算的)值。我添加了一个合适的标题来显示这些值(用于调试目的):
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\newcommand{\slopeTriangle}[9]
{
% Calculate auxilliary quantities.
\pgfmathsetmacro{\xA}{#6+(#1+#2)*(#7-#6)}
\pgfmathsetmacro{\yA}{#8+#3*(#9-#8)}
\pgfmathsetmacro{\xB}{#6+#1*(#7-#6)}
\pgfmathsetmacro{\yB}{\yA}
\pgfmathsetmacro{\xC}{\xA}
\pgfmathsetmacro{\yC}{\yA+(\xA-\xB)*#4}
% Define coordinates for \draw.
\coordinate (A) at (axis cs:\xA,\yA);
\coordinate (B) at (axis cs:\xB,\yB);
\coordinate (C) at (axis cs:\xC,\yC);
% Draw slope triangle.
\draw[#5] (A)--(B) node[pos=0.5,anchor=north] {1};
\draw[#5] (B)--(C);
\draw[#5] (C)--(A) node[pos=0.5,anchor=west] {1};
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},
%xmin=-0.1, % WHAT IF I REMOVE THIS?
%xmax=1.1, % WHAT IF I REMOVE THIS?
xlabel=$x$,
ytick={-0.2,0,2,2.2},
%ymin=-0.2, % WHAT IF I REMOVE THIS?
%ymax=2.2, % WHAT IF I REMOVE THIS?
ylabel style={rotate=-90},
ylabel=$y$,
unit vector ratio=2 1 1,
clip=false,
% Show (automatically) computed limits:
title={%
Axis limits are
$
[\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmin}}
:\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmax}}
] \times
[\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymin}}
:\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymax}}
]$
},
]
\addplot[blue,domain=0:1] {x};
\addplot[red,domain=0:1] {2*x};
\pgfplotsextra{
\pgfkeysgetvalue{/pgfplots/xmin}{\xmin}
\pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
\pgfkeysgetvalue{/pgfplots/ymin}{\ymin}
\pgfkeysgetvalue{/pgfplots/ymax}{\ymax}
\slopeTriangle{0.8}{0.1}{0.1}{1}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
}
\end{axis}
\end{tikzpicture}
\end{document}