考虑以下情况。我有三个图,它们的轴基本上相同,我想将它们并排放置。由于为每个图重复轴标签没有意义,所以只有中间的图有 x 轴标签,而外面的图没有。我现在想做的是将这两个图相对于另一个图放置,以便它们正确对齐。但是,我无法将三个不同的数据可视化放在一个 tikz 图片中,因为我不知道如何正确放置它们。如果我把它们放在不同的tikzpicture
环境中,我就无法访问其他环境中的节点或坐标。我知道我可以将三个不同的图嵌套在另一个图中tikzpicture
,但我想避免这种情况。有没有解决方案可以在一个环境中完成所有操作tikzpicture
?
编辑
我的实际目标是不要通过反复试验来实现,因为这是我以前实现的方法。 pgf 手册中提到,有一个预定义的节点data visualization bounding box
。我希望能够使用它来对齐图形,但不知道数据可视化如何将其作为参数。或者是否有另一种使用节点的可能性?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization} %for graphs and pictures
\usetikzlibrary{datavisualization.formats.functions} %for graphs and pictures
%
\begin{document}
%
\begin{tikzpicture}
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
};
\end{tikzpicture}%
%
\begin{tikzpicture}
\datavisualization [
scientific axes,
x axis = {label={x}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
};
\end{tikzpicture}%
%
\begin{tikzpicture}
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
\vspace{1cm}
\begin{tikzpicture}
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
};
%
\datavisualization [
scientific axes,
x axis = {label={x}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
};
%
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
\end{document}
编辑2
我已经使用该[baseline,remember picture]
选项在我的文档中实现了建议的解决方案,但现在出现了问题,即此选项导致我的子图标签混乱。:-/ 有没有更清洁的解决方案?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization} %for graphs and pictures
\usetikzlibrary{datavisualization.formats.functions} %for graphs and pictures
\usepackage{subfigure}
%
\begin{document}
%
\begin{figure}%
\subfigure[]{
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
};
\end{tikzpicture}%
}
%
\subfigure[]{
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={x}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
};
\end{tikzpicture}%
}
%
\subfigure[]{
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
}
%
\end{figure}
\end{document}
答案1
这种方法不使用单个tikzpicture
,但无论如何它可能对您有用。
您可以使用选项访问单独的节点tikzpicture
,并通过将选项传递给remember picture
轻松对齐轴。baseline
tikzpicture
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization} %for graphs and pictures
\usetikzlibrary{datavisualization.formats.functions} %for graphs and pictures
%
\begin{document}
%
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
}
info{
\node(a) at (visualization cs: x=0, y=2) {a};
};
\end{tikzpicture}%
%
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={x}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
}
info{
\node(b) at (visualization cs: x=2, y=-1) {b};
};
\end{tikzpicture}
%
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
%
\begin{tikzpicture}[remember picture,overlay]
\draw[->,orange,bend left](b) to (a);
\end{tikzpicture}
\end{document}
对编辑 2 的回应
自从subfigure 包已弃用,您能改成 吗subcaption
?如果可以,下面的方法似乎可行,但是您必须将标题放在ssubcaptionbox
下面的 es中subfigure
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization} %for graphs and pictures
\usetikzlibrary{datavisualization.formats.functions} %for graphs and pictures
\usepackage{subcaption}
%
\begin{document}
%
\begin{figure}%
\begin{subfigure}[t]{0.33\linewidth}
\centering
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
}
info{
\node(a) at (visualization cs: x=0, y=2) {a};
};
\end{tikzpicture}
\end{subfigure}%
%
\begin{subfigure}[t]{0.33\linewidth}
\centering
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={x}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
}
info{
\node(b) at (visualization cs: x=2, y=-1) {b};
};
\end{tikzpicture}
\end{subfigure}%
%
\begin{subfigure}[t]{0.33\linewidth}
\centering
\begin{tikzpicture}[baseline,remember picture]
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
\begin{tikzpicture}[remember picture,overlay]
\draw[->,orange,bend left](b) to (a);
\end{tikzpicture}
\end{subfigure}%
\subcaptionbox{Caption for foo \label{fig:foo}}[0.33\linewidth]{}%
\subcaptionbox{Caption for bar \label{fig:bar}}[0.33\linewidth]{}%
\subcaptionbox{Caption for baz \label{fig:baz}}[0.33\linewidth]{}%
\caption{Caption for figure \label{fig:whole}}
\end{figure}
References to subfigures \subref{fig:foo}, \subref{fig:bar}, and \subref{fig:baz} in Fig.~\ref{fig:whole}.
\end{document}
答案2
xshift
,yshift
并且shift
似乎可以作为 的参数\datavisualization
。确实需要反复试验才能找到有用的移位长度,但这是一种选择。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization} %for graphs and pictures
\usetikzlibrary{datavisualization.formats.functions} %for graphs and pictures
%
\begin{document}
%
\begin{tikzpicture}
\datavisualization [
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={{$x^2$}}},
visualize as smooth line=one,
one={style={blue}}
]
data [set=one, format=function] {
var x : interval [-3:5];
func y = \value x ;
};
\datavisualization [shift={(3.25,0)},
scientific axes,
x axis = {label={$x$}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=two,
two={style={green}}
]
data [set=two, format=function] {
var x : interval [-3:5];
func y = \value x * \value x/5;
};
\datavisualization [shift={(6.5,0)},
scientific axes,
x axis = {label={}, length=2.5cm},
y axis = {label={}, include value=-3},
visualize as smooth line=three,
three={style={red}}
]
data [set=three, format=function] {
var x : interval [-3:5];
func y = \value x * \value x * \value x/25;
};
\end{tikzpicture}%
\end{document}
pgfplots
作为参考,pgfplots
有一个名为的库groupplots
对此类事情很有用。下面是一个简单的示例。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
%
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=3 by 1,
y descriptions at=edge left,
horizontal sep=3pt
},
width=0.3\linewidth,
height=0.35\linewidth,
no markers,
domain=-3:5,
ymin=-3,ymax=5,
enlargelimits=false
]
\nextgroupplot[ylabel=$x^2$]
\addplot {x};
\nextgroupplot[xlabel=$x$]
\addplot [green] {x^2/5};
\nextgroupplot
\addplot [red] {x^3/25};
\end{groupplot}
\end{tikzpicture}%
\end{document}