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

\node [rectangle, draw] (a) at (0,0) {
\addplot {gauss(0,0.5)};

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

\draw [<->] (a) -- (b);

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
    Commented Jul 26, 2019 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. Commented Jul 26, 2019 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
    Commented Jul 26, 2019 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
    Commented Jul 26, 2019 at 12:10

1 Answer 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).

\addplot[domain=-3:3] {gauss(x,0,0.5)}; 
\node [rectangle,draw,name=b] at (5,-3) {XYZ}; 
\addplot[domain=-3:3] {gauss(x,3,1)}; 
\draw [<->] (a) -- (b); 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .