我对我的代码的以下奇怪行为感到好奇(参见下面的 MNWE)。如果添加了用于填充函数之间的区域的命令,那么坐标A和乙发生了变化。这对我来说很奇怪,因为我正在使用命令pgfplotspointgetcoordinates
获取点 1 和点 2 的正确坐标。发生了什么?
有问题的代码是:
\addplot[fill=gray,opacity=0.4]
fill between[of=gx and fx,soft clip={domain=-1:1}];
并且图形变化太大。图形曲线发生变化,A点和B点的坐标被触碰。
我还发现点坐标受到以下因素的影响:
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
微电子工程协会
\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}