在 tikz 中根据 pgfplot 的轴定位事物

在 tikz 中根据 pgfplot 的轴定位事物

我在 上有一个图形axis,想用 向其中添加描述性文字和符号tikz。但是,我需要通过目视观察文本是否匹配来使文本相对于轴对齐。有没有办法告诉tikz使用相对于轴的对齐。我尝试使用(axis cs:x,y)命令,但没有帮助。

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=10cm,
            height=4cm,
            x axis line style={-stealth},
            y axis line style={-stealth},
            title={this is my title},
            xticklabels={},
            ymax = 1.5, xmax=7.5,
            axis lines*=center,
            ytick={1},
            xlabel={f},
            ylabel={H(f)},
            xlabel near ticks,
            ylabel near ticks,
            every axis x label/.style={at={(current axis.right of origin)},anchor=west},
            every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south}
            ]
            \addplot+[thick,mark=none,const plot]
            coordinates
            {(0,0) (4,1) (5,0) (7,0)};
        \end{axis}
        % I want this to be aligned with axis' 4 and 5
        \draw[<->] (4.0,-0.3) -- (5.0,-0.3)
\end{tikzpicture}
\end{document}

答案1

axis cs在环境外使用axis将不起作用,因为没有当前的axis。因此,只需将\draw命令移动到轴内并添加axis cs:,它就会按照您想要的方式对齐。由于默认使用 1.11 版坐标系,如果pgfplots中指定了 (或更高版本)。axis cscompat=1.11\pgfplotsset

如您所见,默认情况下,放置在轴区域之外的对象会被剪裁掉。您可以通过添加clip=false轴选项来禁用剪裁,但这也会禁用超出定义的轴限制(xminxmaxyminymax)的绘图的剪裁。如果您改为引入clip mode=individual,绘图将被剪裁,而其他\draw命令和类似命令将不是被剪裁。

\documentclass[border=5pt,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=10cm,
            height=4cm,
            x axis line style={-stealth},
            y axis line style={-stealth},
            title={this is my title},
            xticklabels={},
            ymax = 1.5, xmax=7.5,
            axis lines*=center,
            ytick={1},
            xlabel={f},
            ylabel={H(f)},
            xlabel near ticks,
            ylabel near ticks,
            every axis x label/.style={at={(current axis.right of origin)},anchor=west},
            every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
            clip mode=individual
            ]
            \addplot+[thick,mark=none,const plot]
            coordinates
            {(0,0) (4,1) (5,0) (7,0)};

        \draw[<->] (axis cs:4.0,-0.3) -- (axis cs:5.0,-0.3);
        \end{axis}
\end{tikzpicture}

% with compat=1.11 or newer the axis cs coordinate system is used by default
\pgfplotsset{compat=1.11}

    \begin{tikzpicture}
        \begin{axis}[
            width=10cm,
            height=4cm,
            x axis line style={-stealth},
            y axis line style={-stealth},
            title={this is my title},
            xticklabels={},
            ymax = 1.5, xmax=7.5,
            axis lines*=center,
            ytick={1},
            xlabel={f},
            ylabel={H(f)},
            xlabel near ticks,
            ylabel near ticks,
            every axis x label/.style={at={(current axis.right of origin)},anchor=west},
            every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
            clip mode=individual
            ]
            \addplot+[thick,mark=none,const plot]
            coordinates
            {(0,0) (4,1) (5,0) (7,0)};
        \draw[<->] (4.0,-0.3) -- (5.0,-0.3);
        \end{axis}

\end{tikzpicture}
\end{document}

相关内容