我想要一个由 \pgfplot 绘制的表格和一个两列的图形,但从上方对齐:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{tikz,pgfplots,filecontents}
\usetikzlibrary{shapes,shadows,arrows,matrix,trees,mindmap,backgrounds,automata,shapes.arrows}
%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}
\pgfplotstableread{SpRk.dat}{\SpRk}
\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,
]
\addplot[black,densely dotted] plot coordinates {
(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}
答案1
首先,您用来将表格和图形并排放置的机制可能过于复杂。您将两者放在一个表格中,然后表格的每个单元格都是一个tikzpicture
。显然,您只使用左侧的单元格在左侧表格周围绘制了一个圆形框架。我稍后会编辑此答案,以提供另一种编码表格的方法,根据一些作者的说法,这种方法是更好的风格。
尽管如此,由于两个单元格内容都是 s,因此可以快速修复当前代码,tikzpicture
为每个单元格提供baseline
将基线设置在north
图形的选项。这可以通过更改代码中的三行来实现:
\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,
]
\addplot[ ... ]
更新
正如承诺的那样,这是我的看法。主要变化如下:
- 删除了“外部”表格,我猜想,在您的案例中,它用于对齐左侧表格和右侧图表。这不是必需的,您可以简单地将两个环境并排放置,并在
\hfill
它们之间留出一个。 - 删除了左侧
tikzpicture
用于在左侧表格周围放置圆角框架的元素。此更改与下一个更改相关 - 将表格样式改为 Simon Fear 在其软件包中推荐的样式
booktabs
。根据这位作者(以及其他一些人,还有我自己)的说法,表格中过多使用规则是一种不好的样式。垂直规则不是必需的,水平规则只需要将标题与数据分开。我还给[t]
这个表格提供了顶部对齐的选项。 - 删除所有多余的部分
multicolumn
,因为它们仍然是单细胞。 - 将
filecontents
环境和\pgfplotsset
移至序言,以便更容易阅读主文档的布局。
这是最终的代码:
\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
\pgfplotstableread{SpRk.dat}{\SpRk}
\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,
]
\addplot[black,densely dotted] plot coordinates {
(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}
结果如下:
您还可以考虑使用siunitx
包及其S
列类型,以使表中的数字在小数点处对齐。为了实现这一点,您必须将序言更改tabular
为:
{\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
请注意,现在每个标题都需要放在括号中(以防止siunitx
尝试将标题解析为数字并失败的错误)。
更新:Ipsen 在评论中建议了一种为图的不同线条分配颜色的方法。这个想法是从低 n 值到高 n 值有某种“颜色渐变”。这种方法的主要问题是颜色可能难以区分,但由于在这种情况下每个图都有一个标记,所以这个问题并不重要。因此,可能的颜色分配可能是:
\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};