Q:

How can I set the width of a pgfplot to fit that of a tikz matrix?

I'd like to be able to control the width of a pgfplots plot to that of a matrix. The plot should be below the matrix. Ideally the plot's dimension will depend only on the size of the matrix (so if the matrix changes so will the plot). 
The plot and matrix are all part of a bigger tikzpicture that includes other components all positioned relative to each other, so I don't want to have two separate tikzpictures. Here's a minimal example code:
\documentclass{minimal}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
    \matrix[minimum size=4cm] (A) {
        \node{1};          & \node{2};                      \\
        \draw circle (.1); & \fill (-1,-1) rectangle (1,1); \\
    };
    \begin{axis}
        \addplot[red,domain=0:10,samples=25] {x^2};
    \end{axis}        
\end{tikzpicture}

\end{document}

I've also added a picture of what I'd like it to look like (more or less). Note: I could only achieve that by using two separate tikzpictures...

A:

You can create your own measurement style via 
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\tikzset{getwedist/.code args={#1 and #2}{% measures horizontal distance [#1.west-#2.east] 
  \pgfextra{\pgfpointdiff{\pgfpointanchor{#1}{west}}{\pgfpointanchor{#2}{east}}%
  \xdef\mylength{\the\csname pgf@x\endcsname}}}
}

\begin{document}

\begin{tikzpicture}
    \matrix[minimum size=4cm,append after command={[getwedist=A and A]}] (A) {
        \node{1};          & \node{2};                      \\
        \draw circle (.1); & \fill (-1,-1) rectangle (1,1); \\
    };
    \begin{axis}[width=\mylength,height=2cm,anchor=north,at=(A.south)]
        \addplot[red,domain=0:10,samples=25] {x^2};
    \end{axis}        
\end{tikzpicture}

\end{document}

And please have a look at Why should the minimal class be avoided?

