我正在尝试使用 pgfplots 绘制垂直于曲面的矢量。这是我的 MWE 和结果:
\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\tikzset{test/.style={
postaction={decorate},
decoration={
markings,
mark=between positions 0.0 and 0.1 step 0.01
with { \draw[-latex] (0,0) -- (0,-5); }
}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, ymin=0, xmax=50, ymax=50,
unit vector ratio*=1 1 1]
\addplot[smooth cycle, test] coordinates {
(0.0, 0.0)
(20.0, 15.0)
(40.0, 30.0)
(0.0, 40.0)};
\end{axis};
\end{tikzpicture}
\end{document}
但是,如果我改变轴限制,例如设置
\begin{axis}[
xmin=-20, ymin=0, xmax=50, ymax=50,
unit vector ratio*=1 1 1]
我得到以下输出:
我也通过将一些数据坐标设置为负值来获得类似的行为,例如
\addplot[smooth cycle, test] coordinates {
(0.0, 0.0)
(20.0, 15.0)
(40.0, 30.0)
(-20.0, 40.0)};
问题是如何正确使用 \draw 来装饰 pgfplots 轴环境?
答案1
装饰设置中的坐标需要相对坐标系,axis direction cs
(或rel axis cs
),默认坐标系为绝对坐标,即 axis cs
。
\draw[-latex] (axis direction cs:0,0) -- (axis direction cs:0,-5);
完整示例:
\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\tikzset{test/.style={
postaction={decorate},
decoration={
markings,
mark=between positions 0.0 and 0.1 step 0.01
with {
\draw[-latex] (axis direction cs:0,0) -- (axis direction cs:0,-5);
}
}
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-20, ymin=-5, xmax=50, ymax=50,
% xmin=0, ymin=0, xmax=50, ymax=50,
unit vector ratio*=1 1 1]
\addplot[smooth cycle, test] coordinates {
(0.0, 0.0)
(20.0, 15.0)
(40.0, 30.0)
(0.0, 40.0)};
\end{axis};
\end{tikzpicture}
\end{document}