# 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}
\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...

-

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)]

@12.yakir Related to the minimal class: The MWE package has in fact served me well to replicate images of arbitrary dimensions. :-) If you're bent on using MWE-specific stuff to post questions here. –  1010011010 Jun 17 '14 at 8:06