2

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...

goal

12.yakir
  • 419

1 Answers1

4

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}

enter image description here

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

percusse
  • 157,807
  • @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 08:06
  • Thanks! I'll stick to article then (or try that MWE package). Your answer worked! – 12.yakir Jun 17 '14 at 08:33