tikz 中的警告(“第一个输入路径为空”)

tikz 中的警告(“第一个输入路径为空”)

我正在尝试填充两条曲线之间的区域。理想情况下,我想要做的已经得到解答这里,但我收到以下警告(两次):

Package pgf Warning: fill between skipped: the first input path is empty. on input line 48.

这是我迄今为止尝试过的

\documentclass{article}
\usepackage{tikz,graphicx}
\usepackage{pgfplots, pgfplotstable}
    \usepgfplotslibrary{statistics}
    \usepgfplotslibrary{fillbetween}
    \usetikzlibrary{patterns,intersections}
    \pgfplotsset{compat=newest}
    \usetikzlibrary{babel}
\begin{document}
            \begin{figure}%[ht]
                \centering
                \begin{tikzpicture}
                    \begin{axis}[
                        xmin=-2,xmax=7,
                        ymin=0,ymax=1.5,
                        axis x line=middle,
                        axis y line=middle,
                        axis line style=-,
                        xlabel={$x$},
                        ylabel={$y$},
                        ]
                        % parte negra de f1
                        \addplot[no marks,black,-] expression[domain=-2:3,samples=100]{exp(-0.5*(x-2)^2)} 
                                    node[pos=-0.5,anchor=north east]{};
                        
                        % parte negra de f2
                        \addplot[no marks,black,-] expression[domain=3:7,samples=100]{exp(-0.5*(x-4)^2)} 
                                    node[pos=-0.5,anchor=north east]{};

                        % parte azul de f1
                        \addplot+[no marks,blue,thick,-] [name path=f1,domain=3:7,samples=1000]{exp(-0.5*(x-2)^2)} 
                                    node[pos=0,anchor=north east]{};
                        
                        % parte azul de f2
                        \addplot[no marks,blue,thick,-] expression[name path=f2,domain=-2:3,samples=1000]{exp(-0.5*(x-4)^2)} 
                                    node[pos=0,anchor=north east]{};

                        \draw (2,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk1){$f_1(x)$};
                        
                        \draw (4,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk2){$f_2(x)$};

                        % eje x
                        \addplot[name path=xAxis,const plot,thick,no marks,draw=none] coordinates {(0,0) (1,0)};

                        \addplot[blue!20!white] fill between[of=f1 and xAxis,soft clip={domain=3:7}];
                        \addplot[blue!20!white] fill between[of=f2 and xAxis,soft clip={domain=-2:3}];

                    \end{axis}
                \end{tikzpicture}
                \caption{Función logística o sigmoidal}
                \label{fig:Two plots}
            \end{figure}
\end{document}

应该呈现

在此处输入图片描述

蓝色部分下方的区域已填充。作为奖励,我希望 x 标签不会与曲线相撞。有什么想法可以添加“name path global=axis”吗?

答案1

我认为我可以提出一种更简单的方法来实现您的目标。有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{
        % use this `compat' level or higher so you don't need to specify
        % `axis cs:' any more at each TikZ coordinate
        compat=1.11,
        % declare the functions here so it is easy to calculate the positions
        % of the labels as well
        /pgf/declare function={
            f1(\x) = exp(-0.5*(\x-2)^2);
            f2(\x) = exp(-0.5*(\x-4)^2);
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=-2,xmax=7,
        ymin=0,ymax=1.5,
        % I changed that from `middle' to `bottom', so that also the 0
        % is shown as label ...
        axis x line=bottom,
        axis y line=middle,
        axis line style=-,
        xlabel={$x$},
        ylabel={$y$},
        xlabel style={
            % ... but because of that you need to re-position `xlabel' and
            % the corresponding node anchor if it should be on the end of
            % the x-axis
            at={(xticklabel cs:1)},
            anchor=south east,
        },
        % use this key to draw the axis lines over the other stuff
        axis on top,
        % moved all common `\addplot' options here
        no markers,
        domain=-2:7,
        samples=101,
    ]
        % you only need to plot the functions once
        % (and of course you can use the above defined functions here)
        \addplot [name path=f1] {f1(x)};
        \addplot [name path=f2] {f2(x)};

        % and of course you can also use the declared functions to calculate
        % the proper position of the labels
        % (as stated above, using `compat=1.11' or higher you don't need
        %  to state, e.g. `(axis cs:2,{f1(2)})' any more)
        \node [anchor=south] at (2,{f1(2)}) {$f_1(x)$};
        \node [anchor=south] at (4,{f2(4)}) {$f_2(x)$};

        % even simpler than using an `\addplot' command here is just
        % specifying a path
        % You can use the following line when you explicitly have set the
        % `xmin' and `xmax' values. ...
        % (Of course you can also call this values in `\addplot coordinates')
        \path [name path=xAxis] (\pgfkeysvalueof{/pgfplots/xmin},0)
            -- (\pgfkeysvalueof{/pgfplots/xmax},0);
%        % ... In case `xmin' and `xmax' are not specified, you can use
%        % the following to achieve the same result
%            \path (rel axis cs:0,0) |- (0,0) node [coordinate] (xmin) {};
%            \path (rel axis cs:1,0) |- (0,0) node [coordinate] (xmax) {};
%        \path [name path=xAxis] (xmin) -- (xmax);

        % now you can immediatly draw the thick black line using the
        % `sequence' feature of the `fillbetween' library to which you also
        % give a name to later use it to fill the area below it
        \draw [
            name path=lower,
            draw=blue,
            thick,
            % this is an option from the `fillbetween' library of PGFPlots
            % to create a path along the curves from intersection point to
            % intersection point
            intersection segments={
                of=f1 and f2,
                sequence=R1 -- L2,
            },
        ];

        % doing the stuff above now it is very easy to fill the area under
        % the blue line
        \addplot [blue!20!white] fill between [of=lower and xAxis];

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

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

答案2

我认为您只想剪辑第二条路径。

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics,fillbetween}
\usetikzlibrary{patterns,intersections}
\pgfplotsset{compat=newest}
\usetikzlibrary{babel}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xmin=-2,xmax=7,
    ymin=0,ymax=1.5,
    axis x line=middle,
    axis y line=middle,
    axis line style=-,
    xlabel={$x$},
    ylabel={$y$},
    ]
    % parte negra de f1
    \addplot[no marks, black, -] expression [domain=-2:3, samples=100] {exp(-0.5*(x-2)^2)} node [pos=-0.5,anchor=north east]{};
%     % parte negra de f2
    \addplot[no marks, black, -] expression [domain=3:7, samples=100] {exp(-0.5*(x-4)^2)} node [pos=-0.5, anchor=north east]{};
    parte azul de f1
    \addplot+[no marks, blue, thick, -, name path=f1, domain=3:7, samples=1000] expression [] {exp(-0.5*(x-2)^2)} node [pos=0,anchor=north east]{};
    % parte azul de f2
    \addplot+[no marks, blue,thick, -, name path=f2, domain=-2:3, samples=1000] expression [] {exp(-0.5*(x-4)^2)} node [pos=0, anchor=north east]{};

    \draw (2,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk1){$f_1(x)$};
    \draw (4,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk2){$f_2(x)$};
    % manual p. 429
    \path [name path=xAxis] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);

    % manual p. 431
    \addplot [blue!20!white] fill between[of=f1 and xAxis, soft clip second={domain=3:7}];
    \addplot [blue!20!white] fill between[of=f2 and xAxis, soft clip second={domain=-2:3}];
  \end{axis}
\end{tikzpicture}
\end{document}

剪辑到第二条路径

相关内容