在 PGFPlots 中混合坐标系

在 PGFPlots 中混合坐标系

我尝试理解 pgfplots 的坐标系,MWE 如下:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=\textwidth, height=0.7\textwidth, restrict x to domain = 1:10, xmin=1, xmax=10, clip=false]
\addplot gnuplot[raw gnuplot, color=red, no marks, thick]{set samples 1000; plot x**2*sin(x)};
\filldraw[blue!50] (rel axis cs:-0.1,1.1) rectangle +(axis cs:2,1);
\end{axis}      
\end{tikzpicture}
\end{document}

实际上,我需要在该图上方绘制一些图形(带有点的函数或表格)和几个图元(线、矩形)。因为我事先不知道最大 y 值,所以我对图元使用相对坐标。但我还需要axis cs:在图元中使用 x 坐标。是否可以混合使用不同的坐标系:\draw (a,b) rectangle (c,d),其中acrel axis cs:,但bdaxis cs:

第二个问题:如何将图元裁剪到与图形相同的 x 限值?在我的 MWE 中,矩形从某个坐标(例如,x=0)开始,我希望它在 x=1 处被裁剪,就像图形一样。如果我使用clip=true这个,将会完全擦除矩形。

答案1

不幸的是,你的问题不太精确,所以我有点猜测你真正想要的是什么。如果这不是你想要的,请相应地修改你的问题。

请查看代码的注释以了解详细信息。

% used PGFPlots v.1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat' level or higher so you don't need to prepend
    % TikZ coordinates by `axis cs:' because it is the default coordinate
    % system then
    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=1,
        xmax=10,
        % use this key--value only, if you really need to draw something
        % outside the axis limits
        clip=false,
        % just in case you cross the axis border, it should be drawn on top
        % of everything
        axis on top,
    ]
        \addplot gnuplot [
            raw gnuplot,
            color=red,
            no marks,
            thick,
            % there is no need for the 1000 samples, the default is fine
            % when used together with `smooth'
            smooth,
        ]{
            % added plot limits here
            plot [1:10] x**2*sin(x);
        };

        \filldraw [blue!50]
            % -----------------------------------------------------------------
            % here is a way how you can mix different coordinate systems
            ({rel axis cs:-0.05,0} |- {axis cs:0,15})
                rectangle
            % (as written above: when using an appropriate `compat' level
            %  there is no need to write `axis cs:')
            ({rel axis cs:0.3,0} |- {0,35})
            % -----------------------------------------------------------------
        ;

        % ---------------------------------------------------------------------
        % use a scope and `\clip' to solve problem b
        % (of course you can also use the above shown method of mixing
        %  coordinate systems here, but in addition you can access the axis
        %  limits and use them directly)
        \begin{scope}
            \clip
                (\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin})
                    rectangle
                (rel axis cs:1,1)
            ;
            \fill [green!50] ({rel axis cs:0.1,0} |- {axis cs:0,-50})
                circle (50pt);
        \end{scope}
        % ---------------------------------------------------------------------
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容