# How I can align a table with a figure from above?

I want to have a table and a figure plotted by \pgfplot in two column but have alignment from above:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgfplots,filecontents}
%opening

\begin{document}

\begin{tabular}{ll}
{\footnotesize
\begin{tikzpicture}
\node (table) [inner sep=0pt,left]
{
\begin{tabular}{l|l|l|l|l|l|l}
& \multicolumn{1}{l|}{$p = 1$} & \multicolumn{1}{l|}{$p = 2$}   &\multicolumn{1}{l|}{$p = 4$}  &
\multicolumn{1}{l|}{$p = 8$} & \multicolumn{1}{l|}{$p = 16$} &  \multicolumn{1}{l}{$p = 32$} \\

$n$ & \multicolumn{1}{l|}{t[s]} & \multicolumn{1}{l|}{t[s]}  & \multicolumn{1}{l|}{t[s]}  &
\multicolumn{1}{l|}{t[s]}   &  \multicolumn{1}{l|}{t[s]} &
\multicolumn{1}{l}{t[s]}
\\\hline
$2^{12}$  &  2.15    &   1.25     &   0.68     &    0.55     &    0.47     &   0.46  \\
$2^{13}$  &  6.13    &   3.41     &   1.32     &    0.91     &    0.68     &   0.56 \\
$2^{14}$  &  14.4    &   7.72     &   3.46     &    1.73     &    1.09     &   0.79 \\
$2^{15}$  &  30.1    &   16.0     &   8.10     &    4.30     &    2.09     &   1.26  \\
$2^{16}$  &  71.3    &   37.7     &   16.61    &    9.70     &    5.23     &   2.43  \\
$2^{17}$  &  175     &   91.7     &   39.45    &    20.18    &    11.61    &   6.01  \\
$2^{18}$  &  355     &   226.8    &   184.3    &    95.41    &    48.65    &   24.41  \\
$2^{19}$  &  719     &   369.9    &   191.8    &    130.1    &    57.12    &   26.54   \\
$2^{20}$  &  1435    &   722.7    &   380.5    &    237.5    &    153.7    &   62.34  \\
\end{tabular}
};
\draw [rounded corners=0.6em] (table.north west) rectangle (table.south east);
\end{tikzpicture}
}
&
\begin{filecontents}{SpRk.dat}
nprocs  N12           N13          N14          N15          N16          N17          N18         N19           N20
1       1            1            1            1            1            1            1            1            1
2       1.7159       1.7971       1.8688       1.8832       1.8912       1.9076       1.9244       1.9343       1.9854
4       3.1618       4.6299       4.1676       3.7228       4.2932       4.4359       3.7175        3.721       3.7708
8       3.8949       6.7511       8.3594       7.0142       7.3493       8.6702       7.2912        5.485       6.0428
16       4.5648       9.0015       13.193         14.4       13.632       15.077        14.53       12.496       9.3358
32       4.6943       10.907       18.184       23.921        29.37       29.133       26.555       26.895       23.016
\end{filecontents}
\pgfplotsset{
small,
tick label style={font=\tiny},
label style={font=\small},
legend style={font=\tiny},
anchor=center
}
\begin{tikzpicture}[scale=1.0,yshift=-2cm]
\begin{axis}[
legend style={at={(0.01,1.0)},
anchor=north west},
ymajorgrids=true,
ytick={1,5,10,15,20,25,32},
xtick={1,5,10,15,20,25,32},
xticklabels={1,2,4,8,16,32},
yticklabels={1,2,4,8,16,32},
xlabel= {$\#$ of processors},
%ylabel= Speedup,
]
(1,1)
(2,2)
(4,4)
(8,8)
(16,16)
(32,32)
};
\addplot [black,thick,mark=x] table [x={nprocs}, y={N12}] {\SpRk};
\addplot [green,thick,mark=+] table [x={nprocs}, y={N14}] {\SpRk};
\addplot [purple,thick,mark=square] table [x={nprocs}, y={N16}] {\SpRk};
\addplot [purple,thick,mark=*] table [x={nprocs}, y={N18}] {\SpRk};
\addplot [red,   thick,mark=triangle] table [x={nprocs}, y={N20}] {\SpRk};
\legend{Ideal,$n=2^{12}$,$n=2^{14}$,$n=2^{16}$,$n=2^{18}$,$n=2^{20}$}
\end{axis}
\end{tikzpicture}
\end{tabular}%

\end{document}


-
Just as a side question: why do you put all the table header entries into multicolumn commands if they are all only one column wide? –  Benedikt Bauer Dec 17 '12 at 8:56

First of all, the mechanism you use to have a table and a figure side-to side is perhaps too convoluted. You put both inside a table, and then each cell of the table is a tikzpicture. Apparently you used the one on the left only to draw a rounded frame around the left table. I will edit later this answer to provide an alternative way to code your table which, according with some authors, is better style.

Nevertheless, a quick fix for your current code, since both cell contents are tikzpictures, would be to give to each one the option baseline to set the baseline at the north of the figure. This can be achieved by changing three lines in your code:

\begin{document}

\begin{tabular}{ll}
{\footnotesize
\begin{tikzpicture}[baseline=(table.north)]  % <---- First one
\node (table) [inner sep=0pt,left]
{

[...]

\pgfplotsset{
small,
tick label style={font=\tiny},
label style={font=\small},
legend style={font=\tiny},
anchor=center
}
\begin{tikzpicture}[baseline=(plot.north), scale=1.0,yshift=-2cm]  % <----- Second one
\begin{axis}[
legend style={at={(0.01,1.0)},
anchor=north west},
ymajorgrids=true,
ytick={1,5,10,15,20,25,32},
xtick={1,5,10,15,20,25,32},
xticklabels={1,2,4,8,16,32},
yticklabels={1,2,4,8,16,32},
xlabel= {$\#$ of processors},
name=plot,                                            % <---- Third one
%ylabel= Speedup,
]


# Update

As promised, there is my take. The main changes are:

1. Removed "outer" table, used in your case, I presume, to align the left table and the right plot. It is not required, you can simply put both environments side-to-side leaving a \hfill between them.
2. Removed left tikzpicture used to put a rounded frame around left table. This change is related to next one
3. Changed the style of the table to the one recommended by Simon Fear in its package booktabs. According to this author (and some others, and myself), the overuse of rules in tables is bad style. Vertical rules are not neccesary, and horizontal rules are only required to separate headers from data. Also I gave option [t] to this table to get top alignment.
4. Removed al superflous multicolumn, since they are single cells nevertheless.
5. Moved the filecontents environment and the \pgfplotsset to the preamble, in order to make easier to read the layout of the main document.

This is the final code:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgfplots,filecontents,booktabs}
\usetikzlibrary{}

% File to plot
\begin{filecontents}{SpRk.dat}
nprocs  N12           N13          N14          N15          N16          N17          N18         N19           N20
1       1            1            1            1            1            1            1            1            1
2       1.7159       1.7971       1.8688       1.8832       1.8912       1.9076       1.9244       1.9343       1.9854
4       3.1618       4.6299       4.1676       3.7228       4.2932       4.4359       3.7175        3.721       3.7708
8       3.8949       6.7511       8.3594       7.0142       7.3493       8.6702       7.2912        5.485       6.0428
16      4.5648       9.0015       13.193         14.4       13.632       15.077        14.53       12.496       9.3358
32      4.6943       10.907       18.184       23.921        29.37       29.133       26.555       26.895       23.016
\end{filecontents}
% pgfplots settings
\pgfplotsset{
small,
tick label style={font=\tiny},
label style={font=\small},
legend style={font=\tiny},
anchor=center
}

% ---------------- Document ---------------------------------
\begin{document}

% Table at left
{\footnotesize
\begin{tabular}[t]{ccccccc} % Use [t] option to put the baseline of the table at [t]op
\toprule
& $p = 1$ & $p = 2$ & $p = 4$  & $p = 8$ & $p = 16$ &  $p = 32$ \\
$n$ & t[s]    & t[s]    & t[s]     & t[s]    & t[s]     &  t[s] \\
\midrule
$2^{12}$  &  2.15    &   1.25     &   0.68     &    0.55     &    0.47     &   0.46  \\
$2^{13}$  &  6.13    &   3.41     &   1.32     &    0.91     &    0.68     &   0.56 \\
$2^{14}$  &  14.4    &   7.72     &   3.46     &    1.73     &    1.09     &   0.79 \\
$2^{15}$  &  30.1    &   16.0     &   8.10     &    4.30     &    2.09     &   1.26  \\
$2^{16}$  &  71.3    &   37.7     &   16.61    &    9.70     &    5.23     &   2.43  \\
$2^{17}$  &  175     &   91.7     &   39.45    &    20.18    &    11.61    &   6.01  \\
$2^{18}$  &  355     &   226.8    &   184.3    &    95.41    &    48.65    &   24.41  \\
$2^{19}$  &  719     &   369.9    &   191.8    &    130.1    &    57.12    &   26.54   \\
$2^{20}$  &  1435    &   722.7    &   380.5    &    237.5    &    153.7    &   62.34  \\
\bottomrule
\end{tabular}
}
\hfill
% Plot at right
\begin{tikzpicture}[baseline=(plot.north), scale=1.0,yshift=-2cm]
\begin{axis}[
legend style={at={(0.01,1.0)},
anchor=north west},
ymajorgrids=true,
ytick={1,5,10,15,20,25,32},
xtick={1,5,10,15,20,25,32},
xticklabels={1,2,4,8,16,32},
yticklabels={1,2,4,8,16,32},
xlabel= {$\#$ of processors},
name=plot,
%ylabel= Speedup,
]
(1,1)
(2,2)
(4,4)
(8,8)
(16,16)
(32,32)
};
\addplot [black,thick,mark=x] table [x={nprocs}, y={N12}] {\SpRk};
\addplot [green,thick,mark=+] table [x={nprocs}, y={N14}] {\SpRk};
\addplot [purple,thick,mark=square] table [x={nprocs}, y={N16}] {\SpRk};
\addplot [purple,thick,mark=*] table [x={nprocs}, y={N18}] {\SpRk};
\addplot [red,   thick,mark=triangle] table [x={nprocs}, y={N20}] {\SpRk};
\legend{Ideal,$n=2^{12}$,$n=2^{14}$,$n=2^{16}$,$n=2^{18}$,$n=2^{20}$}
\end{axis}
\end{tikzpicture}
\end{document}


And this is the result:

You may also consider using siunitx package and its S column type, to have the numbers in the table aligned at the decimal point. In order to get this, you have to change the tabular preamble to:

{\footnotesize
\begin{tabular}[t]{lSSSSSS} % Use [t] option to put the baseline of the table at [t]op
\toprule
& {$p = 1$} & {$p = 2$} & {$p = 4$}  & {$p = 8$} & {$p = 16$} &  {$p = 32$} \\
$n$ & {t[s]}    & {t[s]}    & {t[s]}     & {t[s]}    & {t[s]}     &  {t[s]} \\
\midrule


Note that each header needs to be put in braces now (to prevent siunitx errors which will try to parse headers as numbers, and fail).

UPDATE: Ipsen suggested in a comment a way to assign colors to the different lines of the plot. The idea is to have some kind of "color gradient" from low values of $n$ to high values of $n$. The main problem with this approach is that the colors can be difficult to distinguish, but since in this case each plot bears a mark, this problem is not important. So, a possible color asignment could be:

  \addplot [black,thick,mark=x] table [x={nprocs}, y={N12}] {\SpRk};
\addplot [black!60!red,thick,mark=+] table [x={nprocs}, y={N14}] {\SpRk};
\addplot [black!30!red,thick,mark=square] table [x={nprocs}, y={N16}] {\SpRk};
\addplot [red,thick,mark=*] table [x={nprocs}, y={N18}] {\SpRk};
\addplot [red!60,   thick,mark=triangle] table [x={nprocs}, y={N20}] {\SpRk};


-
Thank you very much! –  Mohammad Dec 17 '12 at 9:10
@Mohammad I updated my answer with some suggestions –  JLDiaz Dec 17 '12 at 9:31
Excellent answer. –  Yiannis Lazarides Dec 17 '12 at 9:36
@JLDiaz, +1 for this very nice answer. Just one small comment, that you could consider, if you feel like it. Wouldn't it be nice to add some meaning to the colors? (E.g. small n color A, large n color B and a mixture of A and B for intermediate n. –  Jesper Ipsen Dec 17 '12 at 9:45