我制作了一个图和一张表,我想将表放在图旁边,我该怎么做?
\begin{tikzpicture}
\begin{axis}[
axis y line=center,
axis x line=middle,
axis equal,
grid=both,
xmax=10,xmin=-10,
ymin=-10,ymax=10,
xlabel=$x$,ylabel=$y$,
xtick={-10,...,10},
ytick={-10,...,10},
width=13cm,
]
\addplot coordinates{(-3,9) (-2,7) (-1,5) (0,3) (1,1) (2,-1) (3,-3)};
\end{axis}
\end{tikzpicture}
\begin{tabular}{c c}
X & Y \\
-3 & 9 \\
-2 & 7 \\
-1 & 5 \\
0 & 3 \\
1 & 1 \\
2 & -1 \\
3 & -3 \\
\end{tabular}
答案1
我认为你不需要为这些添加单独的标题。
如果删除两者之间的段落分隔符(空行),它们将放在同一行上。垂直对齐有点偏离,因为的底部tikzpicture
放在当前行上,的中间也是如此。虽然可以使用可选参数修改tabular
的垂直“锚定” ,例如(和也是允许的,后者是默认值),但最好修改行上的垂直位置。tabular
\begin{tabular}[b]{cc}
t
c
tikzpicture
您需要做两项修改:将baseline
其作为选项添加到tikzpicture
,并添加anchor=center
到的选项中axis
,即
\begin{tikzpicture}[baseline]
\begin{axis}[
...
anchor=center]
另一个选项是将整个放置tabular
在\node
内tikzpicture
。您可以通过将节点名称添加到轴(带有name=somenodename
)并将节点放置在 中来执行此操作\node [right=of somenodename] {\begin{tabular}{cc}..\end{tabular}};
。这需要\usetikzlibrary{positioning}
。
最后一条提示:使用该pgfplotstable
包,您可以输入一次数据,并在图表和表格中使用它们。这样您就不必重复信息。我添加了一个示例。
\documentclass{article}
\usepackage[hmargin=2cm]{geometry}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{positioning}
\pgfplotstableread{
X Y
-3 9
-2 7
-1 5
0 3
1 1
2 -1
3 -3
}\mydata
\usepackage{booktabs}
\pgfplotstableset{every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule}}
\begin{document}
\begin{tikzpicture}[baseline]
\begin{axis}[
axis y line=center,
axis x line=middle,
axis equal,
grid=both,
xmax=10,xmin=-10,
ymin=-10,ymax=10,
xlabel=$x$,ylabel=$y$,
xtick={-10,...,10},
ytick={-10,...,10},
width=13cm,
anchor=center
]
\addplot coordinates{(-3,9) (-2,7) (-1,5) (0,3) (1,1) (2,-1) (3,-3)};
\end{axis}
\end{tikzpicture}
\begin{tabular}{c c}
X & Y \\
-3 & 9 \\
-2 & 7 \\
-1 & 5 \\
0 & 3 \\
1 & 1 \\
2 & -1 \\
3 & -3 \\
\end{tabular}
\newpage
\begin{tikzpicture}
\begin{axis}[
axis y line=center,
axis x line=middle,
axis equal,
grid=both,
xmax=10,xmin=-10,
ymin=-10,ymax=10,
xlabel=$x$,ylabel=$y$,
xtick={-10,...,10},
ytick={-10,...,10},
width=13cm,
name=myplot
]
\addplot table[x=X,y=Y] {\mydata};
\end{axis}
\node [right=of myplot] {\pgfplotstabletypeset\mydata};
\end{tikzpicture}
\newpage
\begin{tikzpicture}[baseline]
\begin{axis}[
axis y line=center,
axis x line=middle,
axis equal,
grid=both,
xmax=10,xmin=-10,
ymin=-10,ymax=10,
xlabel=$x$,ylabel=$y$,
xtick={-10,...,10},
ytick={-10,...,10},
width=13cm,
anchor=center
]
\addplot table[x=X,y=Y] {\mydata};
\end{axis}
\end{tikzpicture}
\pgfplotstabletypeset\mydata
\end{document}
上面代码中第二种替代方案的截图: