1

I would like to use \begin{axis} and \addplot within a node, as follows:

\begin{tikzpicture}[xscale=0.5,yscale=0.5]
\pgfmathdeclarefunction{gauss}{2}{
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
}
\node [rectangle, draw] (a) at (0,0) {
\begin{axis}[height=3cm,width=5cm]
\addplot {gauss(0,0.5)};
\end{axis}
};

\node [rectangle, draw] (b) at (5,-2) {Test};

\draw [<->] (a) -- (b);
\end{tikzpicture} 

The rectangle unfortunately does not surround the plot. How can that be fixed?

What is the correct way to include a plot in a node?

Thanks a lot!

  • 1
    Welcome. An axis is a node. What you are trying to do is to nest tikzpictures, which is to be avoided. If you only want to add a frame around the axis node, just add \draw (current axis.south west) rectangle (current axis.north east); after \end{axis} before \end{tikzpicture}. (There are many other possibilities.) – user121799 Jul 26 at 9:03
  • 1
    I would say the correct way is to not place the axis in a node. If you want the axis at a specific location, and/or refer to its anchors, use \begin{axis}[at={(x,y)}, name=foo, ...] (the default anchor is south west, so the bottom left corner of the axis is placed at (x,y)). With name=foo you can use e.g. (foo) or (foo.north) as a coordinate. – Torbjørn T. Jul 26 at 9:35
  • Thanks a lot. The fact that an axis is simply a node makes things more logical in my head. I have updated my example as follows: \begin{tikzpicture} \begin{axis}[name=a,width=4cm,at={(0,0)}] \addplot {gauss(0,0.5)}; \end{axis} \node [rectangle,draw,name=b] at (5,-3) {XYZ}; \begin{axis}[name=c,width=4cm,at={(7,0)}] \addplot {gauss(3,1)}; \end{axis} \draw [<->] (a) -- (b); \end{tikzpicture} Strangely the arrow is not in the right place, connecting a with b. Do you see why this is? – Chris Jul 26 at 11:07
  • Try \documentclass[border=3.14mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.15} \begin{document} \begin{tikzpicture}[declare function={gauss(\x,\y,\z)=1/(\z*sqrt(2*pi))*exp(-((\x-\y)^2)/(2*\z^2));}] \begin{axis}[name=a,width=4cm] \addplot[domain=-3:3] {gauss(x,0,0.5)}; \end{axis} \node [rectangle,draw,name=b] at (5,-3) {XYZ}; \begin{axis}[name=c,width=4cm,xshift=7cm] \addplot[domain=-3:3] {gauss(x,3,1)}; \end{axis} \draw [<->] (a) -- (b); \end{tikzpicture} \end{document} – user121799 Jul 26 at 12:10
1

This is more to familiarize you with the format. If you want to respond to a comment, then you need to address the user you want to address. So in your above comment you should have added @TorbjørnT. Also you should edit your question instead of adding code pieces in the comments. In any case, the following works. Your approach suffered from spurious spaces in the definition of the Gaussian, and declare function is arguably easier to deal with anyway (but you could just have removed the spaces).

\documentclass[border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[declare
function={gauss(\x,\y,\z)=1/(\z*sqrt(2*pi))*exp(-((\x-\y)^2)/(2*\z^2));}]
\begin{axis}[name=a,width=4cm] 
\addplot[domain=-3:3] {gauss(x,0,0.5)}; 
\end{axis} 
\node [rectangle,draw,name=b] at (5,-3) {XYZ}; 
\begin{axis}[name=c,width=4cm,xshift=7cm] 
\addplot[domain=-3:3] {gauss(x,3,1)}; 
\end{axis} 
\draw [<->] (a) -- (b); 
\end{tikzpicture} 
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.