选择

选择

我在这里找到了一些关于如何ymin/ymax通过 自动计算的答案pgfplots。可以添加到axis设置中after end axis/.code={\pgfmathsetmacro{\hi}{\pgfkeysvalueof{/pgfplots/ymax}}},对于 也类似ymin。我的 MWE 是:

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\begin{document}

\def\xmi{0}
\def\xma{3.5}

\begin{tikzpicture}
\begin{axis}[xlabel={x},ylabel={y}, no markers, samples=100, clip mode=individual, xmin=\xmi, xmax=\xma, restrict x to domain=\xmi:\xma,
after end axis/.code={
\pgfmathsetmacro{\hi}{\pgfkeysvalueof{/pgfplots/ymax}};
\pgfmathsetmacro{\lo}{\pgfkeysvalueof{/pgfplots/ymin}};
\xdef\hhi{1.2*\hi}; % tried this, but it gives error further in node{test}
\fill[green] ($(\xmi,\hi + 0.05*\hi - 0.05*\lo)$) rectangle ($(\xma,\hi + 0.1*\hi - 0.1*\lo)$);
\node at (\xma/2,\hi) {node1};
}
]

\addplot {6*sin(deg(x))};

\node at (2,8) {text};

%\node at ($(\xma/2, \hhi)$) {test}; % this gives error

\end{axis}
\end{tikzpicture}
\end{document}

我看不到图上方的绿色矩形,并且{node1}位置错误。注释节点也{test}给出错误。为什么sin不从x=0确切的位置开始?我还看到使用restrict ...可能会改变整个图片。我错在哪里?谢谢。

答案1

似乎您实际上需要axis cs使用 中的轴坐标after end axis,即使有compat=1.15。您确实需要不是但是坐标中需要美元符号,它们不需要像您一样在坐标的组件中进行算术运算。(阅读 TikZ 手册中的第 13.5 节,了解您可以将该语法用于什么。)

绿色矩形也出现在您的代码中。我怀疑坐标的分量被读取为以点为单位的长度。因此,例如,矩形的端点位于大约(3.5pt, 7.8pt)

关于\xdefed 宏,我认为你试图在它尚未定义时使用它,\hhi它将仅可用 \end{axis}1.2*6.80955100000. (当然,它将扩展为,而不是该计算的结果值。)

最后,正弦曲线不从零开始的原因是造成混淆的一个常见原因。默认域是 -5:5,使用 100 个样本,您会在 x=-0.05 处得到一个点,在 x=0.05 处得到下一个点。由于您的restrict x to domain设置,-0.05 处的点被丢弃(实际上设置为-inf),因此曲线从 0.05 开始。在这里您可能希望使用domain=\xmi:\xma而不是restrict to domain

代码输出

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\newcommand\xmi{0}
\newcommand\xma{3.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xlabel={x},
  ylabel={y},
  no markers,
  samples=100,
  clip=false,
  xmin=\xmi,
  xmax=\xma,
  domain=\xmi:\xma,
  after end axis/.code={
    \pgfmathsetmacro{\hi}{\pgfkeysvalueof{/pgfplots/ymax}};
    \pgfmathsetmacro{\lo}{\pgfkeysvalueof{/pgfplots/ymin}};
    % note axis cs: in the coordinates in the next two lines
    \fill[green,ultra thick] (axis cs:\xmi,\hi+0.05*\hi-0.05*\lo) rectangle (axis cs:\xma,\hi+0.1*\hi-0.1*\lo);
    \node at (axis cs:\xma/2,\hi) {node1};
  }
]

\addplot {6*sin(deg(x))};

\node at (2,8) {text};

\end{axis}
\end{tikzpicture}
\end{document}

选择

有几种不同的方法可以绘制这样的矩形,使用calc库的功能。这依赖于提供axis a name,以便可以访问其锚点。

第二个代码块的输出

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\newcommand\xmi{0}
\newcommand\xma{3.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  % note addition of name=ax
  name=ax,
  xlabel={x},
  ylabel={y},
  no markers,
  samples=100,
  xmin=\xmi,
  xmax=\xma,
  domain=\xmi:\xma,
]

\addplot {6*sin(deg(x))};

\node at (2,8) {text};
\end{axis}

\path
 % the let syntax requires \usetikzlibrary{calc}
 let
  % with \p1=(ax.south west), the x-component of the coordinate of
  % the bottom left corner of the axis is available in \x1,
  % and the y-component in \y1
  \p1=(ax.south west),
  % similarly for the top right corner
  \p2=(ax.north east),
  % the value of a calculation can be saved to \n{<something>}
  \n1={0.05*(\y2-\y1)}
  % note that the macros defined above are not available
  % outside this specific path
  in
  % save coordinates for the rectangle
  % (you could also have used \fill let ... (\x1,\y2+\n1) rectangle (\x2,\y2+2*\n1); directly)
  coordinate (lowerleft) at (\x1,\y2+\n1)
  coordinate (upperright) at (\x2,\y2+2*\n1)
;

\fill[green] (lowerleft) rectangle (upperright);

% the anchors of the axis can be used to place stuff
\node at (ax.north) {node1};

% another possibility, again with the calc-library
% ($(ax.south west)!1.15!(ax.north west)$) is using a "partway modifier"
% draw a line from ax.south west to ax.north west, then make it 15% longer
% (multiply length by 1.15), this coordinate is at that point
\fill [blue] ($(ax.south west)!1.15!(ax.north west)$) rectangle 
             ($(ax.south east)!1.2!(ax.north east)$);
\end{tikzpicture}
\end{document} 

方案 2

rel axis cs坐标系,可让您将坐标指定为轴坐标系的分数。(rel axis cs:0,0)是轴的左下方,(rel axis cs:1,1)是右上角。

rel axis cs可以与axis cs使用垂直坐标规范的坐标相结合,参见第 1 点TikZ:箭头的 |- 符号到底起什么作用?,以及下面的代码示例。

第三个代码块的输出

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\newcommand\xmi{0}
\newcommand\xma{3.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  % note addition of name=ax
  name=ax,
  xlabel={x},
  ylabel={y},
  no markers,
  clip mode=individual,
  samples=100,
  xmin=\xmi,
  xmax=\xma,
  domain=\xmi:\xma,
]

\addplot {6*sin(deg(x))};

\node at (2,8) {text};

% you can use relative axis coordinates
\fill [green] (rel axis cs:0,1.05) rectangle (rel axis cs:1,1.1);

% to combine relative and explicit coordinates
\fill [red] ({rel axis cs:0,1.15} -| {axis cs:0.5,0}) % y-component of (rel axis cs:0,1.15), x-component of (axis cs:0.5,0)
            rectangle
            ({rel axis cs:0,1.2} -| {axis cs:2,0});

\fill [blue] ({rel axis cs:0,1.15} -| {axis cs:2.5,0}) rectangle
              ({rel axis cs:0,1.2} -| {axis cs:\xma,0});
\end{axis}
\end{tikzpicture}
\end{document} 

相关内容