\pgfplotspointgetcoordinates 在 fillbetween 域中的问题

\pgfplotspointgetcoordinates 在 fillbetween 域中的问题

我有以下 MWE:

\documentclass[tikz]{standalone}
\usepackage{amsmath,amssymb}                    
\usepackage{color,xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\newcommand{\InterseccaoX}[1]{
                        \pgfplotspointgetcoordinates{#1} 
                        \pgfmathprintnumber[fixed,precision=2]{
                             \pgfkeysvalueof{/data point/x}
                        }
}

\newcommand{\InterseccaoY}[1]{
                        \pgfplotspointgetcoordinates{#1} 
                        \pgfmathprintnumber[fixed,precision=2]{
                             \pgfkeysvalueof{/data point/y}
                        }
}

\newcommand{\ValorInterseccao}[1]{
\pgfplotspointgetcoordinates{#1}\pgfkeysvalueof{/data point/x}
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[domain=-2:2, samples=100,axis lines=middle,ymin=-0.5,xmin=-3,xmax=3]


  % Desenhe a curva f(x)
  \addplot[name path=f, blue] {exp(-x^2)}; 

  % Desenhe a curva g(x)
  \addplot[name path=g, red] {0.5*x^2};

  % Encontre os pontos de interseção
  \path[name intersections={of=f and g, name=i}];

  \fill (i-1) circle (2pt);
  \fill (i-2) circle (2pt);

  % Preencha a área entre as curvas apenas entre os pontos de interseção
   \node[below=15pt,inner sep=2pt] (Ia) at ($(0,0)!(i-1)!(-1,0)$) {\InterseccaoX{(i-1)}};
   \node[below=15pt,inner sep=2pt] (Ib) at ($(0,0)!(i-2)!(-1,0)$) {\InterseccaoX{(i-2)}};
   \node[below,inner sep=1pt] (Ic) at ($(0,0)!(i-2)!(0,1)$) {\InterseccaoY{(i-2)}};

   \draw[densely dotted] (Ib) -- (i-2) -- (i-1) -- (Ia);
\addplot[fill=gray,opacity=0.4] fill between[of=f and g,soft clip={domain=-0.92:0.92}];
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

我已经确定了这些曲线的交点。没问题。但是,当我尝试使用以下指令填充曲线之间的区域时:

\addplot[fill=gray,opacity=0.4] fill between[of=f and g,soft clip={domain=-0.92:0.92}];

我需要提供交点的 x 值 ( -0.92:0.92)。有没有办法我给出命令然后自动插入 x 交点?例如,我尝试过:

domain=\pgfplotspointgetcoordinates{(i-1)}\pgfkeysvalueof{/data point/x}:\pgfplotspointgetcoordinates{(i-2)}\pgfkeysvalueof{/data point/x}

但不起作用。有人能帮助我吗?

答案1

由于您已经加载了fillbetween库,您可以使用该intersection segments选项并完全避免单独计算坐标:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc}

\newcommand{\InterseccaoX}[1]{
    \pgfplotspointgetcoordinates{#1} 
    \pgfmathprintnumber[fixed, precision=2]{
         \pgfkeysvalueof{/data point/x}
    }
}

\newcommand{\InterseccaoY}[1]{
    \pgfplotspointgetcoordinates{#1} 
    \pgfmathprintnumber[fixed, precision=2]{
         \pgfkeysvalueof{/data point/y}
    }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    domain=-2:2, 
    samples=100,
    axis lines=middle,
    ymin=-0.5, 
    xmin=-3,
    xmax=3
]

    % Desenhe a curva f(x)
    \addplot[name path=f, blue] {exp(-x^2)}; 
    
    % Desenhe a curva g(x)
    \addplot[name path=g, red] {0.5*x^2};
    
    % Encontre os pontos de interseção
    \path[name intersections={of=f and g, name=i}];
    
    \fill (i-1) circle (2pt);
    \fill (i-2) circle (2pt);

    % Preencha a área entre as curvas apenas entre os pontos de interseção
    \node[below=15pt, inner sep=2pt] (Ia) at ($(0,0)!(i-1)!(-1,0)$)   
        {\InterseccaoX{(i-1)}};
    \node[below=15pt, inner sep=2pt] (Ib) at ($(0,0)!(i-2)!(-1,0)$) 
        {\InterseccaoX{(i-2)}};
    \node[below, inner sep=1pt] (Ic) at ($(0,0)!(i-2)!(0,1)$) 
        {\InterseccaoY{(i-2)}};
    
    \draw[densely dotted] (Ib) -- (i-2) -- (i-1) -- (Ia);

    \fill[gray, opacity=0.4, intersection segments={
        of=f and g,
        sequence={L2 -- R2[reverse]},
    }] -- cycle;
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述


另一种解决方案可能是不仅给出一个domain路径soft path,而且还给出一条实际路径,您可以在其中使用预先计算的交叉点坐标。

您可以在此处使用语法-|。以下代码

(0,\pgfkeysvalueof{/pgfplots/ymin} -| i-1) rectangle 
(0,\pgfkeysvalueof{/pgfplots/ymax} -| i-2) 

在下面的例子中意味着:返回一个矩形,其中

  • 第一个坐标有X(i-1)坐标和 a相同的值与最小值相同的值当前地块的价值,
  • 第二个坐标有X(i-2)坐标和 a相同的值与最大值相同的值当前地块的价值。

使用\pgfkeysvalueof{/pgfplots/ymin}\pgfkeysvalueof{/pgfplots/ymax}确保生成的区域覆盖整个地块的高度。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc}

\newcommand{\InterseccaoX}[1]{
    \pgfplotspointgetcoordinates{#1} 
    \pgfmathprintnumber[fixed, precision=2]{
         \pgfkeysvalueof{/data point/x}
    }
}

\newcommand{\InterseccaoY}[1]{
    \pgfplotspointgetcoordinates{#1} 
    \pgfmathprintnumber[fixed, precision=2]{
         \pgfkeysvalueof{/data point/y}
    }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    domain=-2:2, 
    samples=100,
    axis lines=middle,
    ymin=-0.5, 
    xmin=-3,
    xmax=3
]

    % Desenhe a curva f(x)
    \addplot[name path=f, blue] {exp(-x^2)}; 
    
    % Desenhe a curva g(x)
    \addplot[name path=g, red] {0.5*x^2};
    
    % Encontre os pontos de interseção
    \path[name intersections={of=f and g, name=i}];
    
    \fill (i-1) circle (2pt);
    \fill (i-2) circle (2pt);

    % Preencha a área entre as curvas apenas entre os pontos de interseção
    \node[below=15pt, inner sep=2pt] (Ia) at ($(0,0)!(i-1)!(-1,0)$)   
        {\InterseccaoX{(i-1)}};
    \node[below=15pt, inner sep=2pt] (Ib) at ($(0,0)!(i-2)!(-1,0)$) 
        {\InterseccaoX{(i-2)}};
    \node[below, inner sep=1pt] (Ic) at ($(0,0)!(i-2)!(0,1)$) 
        {\InterseccaoY{(i-2)}};
    
    \draw[densely dotted] (Ib) -- (i-2) -- (i-1) -- (Ia);

    \addplot[fill=gray, opacity=0.4] 
        fill between[of=f and g, soft clip={
            (0,\pgfkeysvalueof{/pgfplots/ymin} -| i-1) rectangle 
            (0,\pgfkeysvalueof{/pgfplots/ymax} -| i-2)
        }
    ];
\end{axis}
\end{tikzpicture}

\end{document}

输出与上面相同。

相关内容