2

I'm curios with following strange behavior of my code (see MNWE below). If the command for filling area between function is added, than the coordinates A and B are changed. It is weird for me, because I am using the command pgfplotspointgetcoordinates for obtaining the correct coordinates for point-1 and point-2. What is going on?

correct graph without fill command

The problematic code is:

  \addplot[fill=gray,opacity=0.4] 
    fill between[of=gx and fx,soft clip={domain=-1:1}];

And the figure is changed too much. The graph curves are changed and the coordinates for point A and B is touched.

I also see that the point coordinates are influenced by:

  enlarge y limits={rel=0.13},
  enlarge x limits={rel=0.07},

wrong graph with active fill command

MNWE

\documentclass[11pt]{standalone}
\usepackage[usenames,x11names]{xcolor}
\usepackage{pgfplots}
  \pgfplotsset{compat=newest}
  \usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\usepackage{amsmath}

\begin{document} \begin{tikzpicture}[ thick,scale=0.5, every node/.style={transform shape} ] \begin{axis}[ domain = -2:2, restrict y to domain=0:4, grid = major, % both grid style={line width=.2pt, draw=gray!20}, major grid style={dashed, line width=.2pt, draw=gray!40}, minor tick num=5, clip = true, clip mode=individual, axis x line = middle, x axis line style={name path=xaxis}, % https://tex.stackexchange.com/questions/574555/tikz-refer-to-intersection-with-x-axis axis y line = middle, xlabel={(x)}, ylabel={(y)}, enlarge y limits={rel=0.13}, enlarge x limits={rel=0.07}, ]

  \addplot[
    name path global=fx,
    color=Gold3, samples=20, 
    smooth, ultra thick, 
    unbounded coords=jump, 
    no markers
  ]  gnuplot{2/(1+x^2)}; 

  \addplot[
    name path global=gx,
    color=Coral4, samples=20, 
    smooth, ultra thick, 
    unbounded coords=jump, 
    no markers
  ] gnuplot{x^2};  

  % fill area bounded by two curves
  % https://tex.stackexchange.com/questions/38461/
  \addplot[fill=gray,opacity=0.4] 
    fill between[of=gx and fx,soft clip={domain=-1:1}];

  \path [name intersections={of=fx and gx, name=point}];
  \node (prusecikA) [left,  font=\scriptsize] 
    at (point-1) {
    % -------------------------------------------------------------
    % using '\pgfplotspointgetcoordinates' stores the (axis)
    % coordinates of e.g. the coordinate (point-2) in
    % 'data point', which then can be called by '\pgfkeysvalueof'
      \pgfplotspointgetcoordinates{(point-1)}
      \(A=[
          \pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/x}},
          \pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/y}}
      ]\)
    };
  \node (prusecikB) [right,  font=\scriptsize] 
    at (point-2) {
      \pgfplotspointgetcoordinates{(point-2)}
      \(B=[
          \pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/x}},
          \pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/y}}
      ]\)
    };


  \draw[dashed] (-1,0) -- (point-1);
  \draw[dashed] (+1,0) -- (point-2);
  \node[below left] at (0,0) {\(0\)};
  \node[left, Gold3] at (1.5,2.3) {\(f(x) = \frac{2}{1-x^2}\)};
  \node[right, Coral4] at (-2,4) {\(g(x) = x^2\)};
  \fill [black] (point-1) circle (1mm);
  \fill [black] (point-2) circle (1mm);
\end{axis}

\end{tikzpicture} \end{document}

JardaFait
  • 3,922

0 Answers0