addplot 域中的值变量

addplot 域中的值变量

在此 MWE 中

\documentclass[border=1cm, x11names,tikz=true]{standalone}

\usepackage{pgfplots} 
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections, calc}
\usepackage[round-mode=places]{siunitx}

\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\newcommand\xcoord[2][center]{{%
   % The actual point of interest
   \pgfpointanchor{#2}{#1}%
   \pgfgetlastxy{\ix}{\iy}%
   % (0,0)
   \pgfplotspointaxisxy{0}{0}%
   \pgfgetlastxy{\ox}{\oy}
   % (1,1)
   \pgfplotspointaxisxy{1}{1}%
   \pgfgetlastxy{\ux}{\uy}
   \pgfmathparse{(\ix-\ox)/(\ux-\ox)}
   \pgfmathprintnumber[fixed, precision=4]{\pgfmathresult}
}}

\begin{document}
\begin{tikzpicture}[line width=1pt, color=black]

\begin{axis}[samples=50,smooth,width=10cm,height=8cm, scale only axis, axis x line=middle, axis y line=middle, hide y axis, xmin=-7, xmax=7, xlabel=$y$, xtick=\empty, ymin=-0.2, ymax=0.3, ytick=\empty]

    \addplot[color=red, domain=-5:5, name path=g1] {0.4*gauss(-3,1)};
    \addplot[color=blue, domain=-5:5, name path=g2] {0.2*gauss(-1,1)};
    \path [name intersections={of=g1 and g2,by=a}];
    \draw[dashed] (a) --  ($(-5,0)!(a)!(5,0)$) node[pos=1, below] {$\gamma_{1}$};
     \node at (a) {$ \xcoord{a}$};  

    \addplot[color=black, domain=-5:-1.6535, line width=1.5pt] {0.4*gauss(-3,1)};
    \addplot[color=black, domain=-1.6535:5, line width=1.5pt] {0.2*gauss(-1,1)};

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

我想将坐标的 x 值a用于第三和第四个域。可以吗?有什么帮助吗?谢谢。

答案1

您可以使用两种axis环境:

\documentclass[border=1cm, x11names,tikz=true]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.13}
\usetikzlibrary{intersections,calc}

\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\newcommand\xcoord[2][center]{{%
   % The actual point of interest
   \pgfpointanchor{#2}{#1}%
   \pgfgetlastxy{\ix}{\iy}%
   % (0,0)
   \pgfplotspointaxisxy{0}{0}%
   \pgfgetlastxy{\ox}{\oy}%
   % (1,1)
   \pgfplotspointaxisxy{1}{1}%
   \pgfgetlastxy{\ux}{\uy}%
   \pgfmathparse{(\ix-\ox)/(\ux-\ox)}%
   \pgfmathprintnumber[fixed, precision=4]{\pgfmathresult}%
   \expandafter\xdef\csname xcoord#2\endcsname{\pgfmathresult}%
}}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  every axis/.append style={
    samples=50,smooth,
    width=10cm,height=8cm, scale only axis,
    axis lines=middle, 
    hide y axis, 
    xmin=-7, xmax=7, xlabel=$y$, xtick=\empty,
    ymin=-0.2, ymax=0.3, ytick=\empty
  }
}
\begin{axis}
    \addplot[color=red, domain=-5:5, name path=g1] {0.4*gauss(-3,1)};
    \addplot[color=blue, domain=-5:5, name path=g2] {0.2*gauss(-1,1)};
    \path [name intersections={of=g1 and g2,by=a}];
    \draw[dashed] (a) -- (a|-0,0) node[below] {$\gamma_{1}$};
    \node at (a){\xcoord{a}};
\end{axis}
\begin{axis}[hide x axis]
    \addplot[color=black,domain=-5:\xcoorda, line width=1.5pt] {0.4*gauss(-3,1)};
    \addplot[color=black, domain=\xcoorda:5, line width=1.5pt] {0.2*gauss(-1,1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here


但我建议使用 pgfplots 库fillbetween

\documentclass[border=1cm, x11names,tikz=true]{standalone}

\usepackage{pgfplots} 
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
  samples=50,smooth,
  width=10cm,height=8cm, scale only axis,
  axis lines=middle, 
  hide y axis, 
  xmin=-7, xmax=7, xlabel=$y$, xtick=\empty,
  ymin=-0.2, ymax=0.3, ytick=\empty
]

\addplot[color=red, domain=-5:5, name path=g1] {0.4*gauss(-3,1)};
\addplot[color=blue, domain=-5:5, name path=g2] {0.2*gauss(-1,1)};

\draw[draw,black,line width=1.5pt,
  intersection segments={
    of=g1 and g2,
    sequence={L1 R2}
  }
];

\path [name intersections={of=g1 and g2,by=a}];
\draw[dashed] (a) -- (0,0 -|a) node[pos=1, below] {$\gamma_{1}$};

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

sequence={L1 R2}表示:使用 中左侧提到的路径的第一段和 中右侧提到的路径的第二段of= g1 and g2。有关更多信息,请参阅 中的 5.7.6 交叉路口段重组记录pgfplots

结果:

enter image description here

相关内容