5

Consider the following situation. I have three plots, where basically the axes are the same and I want to place them side by side. Since it doesn't make sense to repeat the axis labels for every plot, just the middle one has an x-axis label, while the outer ones haven't. What I would like to do now is to place the plots with respect to another, so that they are aligned properly. However, I can't put the three different datavisualizations within one tikz picture, since I have no idea how to place them properly. And if I put them in different tikzpicture environments, I can't access nodes or coordinates from the other environments. I know I could nest the three different plots within another tikzpicture, but I would like to avoid that. Is there a solution to carry out everything within a single tikzpicture environment?

EDIT

My actual goal is to not do it by trial and error, since this was the way I achieved it before. In the pgf manual is mentioned, that there is the predefined node data visualization bounding box. I hoped to being able to align the graphs by using this, but have no idea, how the datavisualization would take this as an argument. Or is there another possibility using nodes?

\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}

enter image description here

enter image description here

EDIT2

I have implemented the suggested solution using the [baseline,remember picture] option into my document, but now the problem arises, that this options leads to a mess up of my subfigure labels. :-/ Is there a cleaner solution?

\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}

enter image description here

JMP
  • 3,238

2 Answers2

3

xshift, yshift and shift seems to work as arguments to a \datavisualization. It does require some trial and error to find useful lengths for shifting, but it's one option.

\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}

enter image description here

pgfplots

For reference, pgfplots has a library called groupplots that is useful for things like this. A quick example below.

\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}

enter image description here

Torbjørn T.
  • 206,688
  • Thanks for this approach, but my actual goal was to not do it by trial and error, since this was the way I achieved it before (actually by adjusting the inner sep of the x-label). In the pgf manual is mentioned, that there is the predefined node data visualization bounding box. I hoped to being able to align the graphs by using this, but have no idea, how the datavisualization would take this as an argument. – JMP Mar 30 '16 at 20:10
  • @JMP I see, and I'm afraid I don't have any suggestions at the moment. Would using pgfplots instead be an option? – Torbjørn T. Mar 30 '16 at 20:31
  • Actually I would like to stick with tikz-datavisualization, since the whole diagram is set up, which is a little more complex, than what is included above. However, as I said, I already achieved my goal by trial and error, but I nevertheless would be interested, if there is a cleaner solution. – JMP Mar 30 '16 at 20:36
3

This approach doesn't use a single tikzpicture, but it may work for you anyway.

You can access nodes in separate tikzpictures with the remember picture option, and easily align the axes by passing the baseline option to 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}

enter image description here

Response to edit 2

Since the subfigure package is deprecated, is it possible for you to change to subcaption? If so, the following appears to work, however you have to place the captions in subcaptionboxes below the subfigures.

\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}

enter image description here

erik
  • 12,673
  • Thanks for this answer. This is interesting. Why is the baseline the same for the graphs without x-label? – JMP Mar 30 '16 at 21:28
  • If I understand correctly, the the plot area is defined by data visualization bounding box, which in this case corresponds to the axis lines (may be different for different axes). The ticks, labels, etc. are positioned with respect to this bounding box, and may lie outside (e.g., the axis label would be below data visualization bounding box.south). Hence the graphs share a baseline, but some have more things below the baseline (like how the tail of a letter y hangs down rather than the character being shifted up). – erik Mar 30 '16 at 21:57
  • I tried to implement your solution into my document, which actually led to a different problem. See my second edit above. – JMP Apr 05 '16 at 18:48
  • @JMP Please see my edit. – erik Apr 05 '16 at 20:46
  • I didn't realized yet, that the subfigure package is deprecated. This will require some work on my document to migrate to subcaption, but however it looks like this needs to be done. Thanks for your answer. – JMP Apr 06 '16 at 07:39