1

I am trying to make two graphs in a horizontal line but I couldn't find any proper method to make it in one row enter image description here someone help me??

\pgfplotsset{compat=newest}
\begin{figure}[!hbt]
\begin{center}

\begin{tikzpicture}
\begin{axis}[
title={A Sinusoidal Sequence of Cartesian Trajectories},
xlabel={$t[\rm{sec}]$},
ymin=-25,ymax=25,
xmin=0,xmax=6,
minor y tick num=1,
samples=3,
legend entries={$\Delta{x}[\rm{mm}]$,$\Delta{y}[\rm{mm}]$,$\Delta{z}[\rm{mm}]$,$\Delta{\psi}[^{\circ}]$,$\Delta{\theta}[^{\circ}]$,$\Delta{\phi}[^{\circ}]$},
legend pos=outer north east
]
\addplot[mark size=0.8,draw=red,smooth] table {Tx.txt};
\addplot[mark size=0.8,draw=magenta,smooth] table {Ty.txt};
\addplot[mark size=0.8,draw=cyan,smooth] table {Tz.txt};
\addplot[mark=*,mark size=0.8,draw=blue,smooth] table {Txr.txt};
\addplot[mark size=0.8,draw=gray] table {Tyr.txt};
\addplot[mark size=0.8,draw=darkgray] table {Tzr.txt};


\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[
title={Corresponding Limb Lengths with White Noise},
xlabel={$t[\rm{sec}]$},
ymin=-25,ymax=25,
xmin=0,xmax=6,
minor y tick num=1,
samples=3,
legend entries={$\Delta{x}[\rm{mm}]$,$\Delta{y}[\rm{mm}]$,$\Delta{z}[\rm{mm}]$,$\Delta{\psi}[^{\circ}]$,$\Delta{\theta}[^{\circ}]$,$\Delta{\phi}[^{\circ}]$},
legend pos=outer north east
]
\addplot[mark size=0.8,draw=red,smooth] table {Txc.txt};
\addplot[mark size=0.8,draw=magenta,smooth] table {Tyc.txt};
\addplot[mark size=0.8,draw=cyan,smooth] table {Tzc.txt};
\addplot[mark=*,mark size=0.8,draw=blue,smooth] table {Trxc.txt};
\addplot[mark size=0.8,draw=gray] table {Tryc.txt};
\addplot[mark size=0.8,draw=darkgray] table {Trzc.txt};

\end{axis}
\end{tikzpicture}
\caption{A sequence of sinusoidal Cartesian trajectories (Upper), and their corresponding limb lengths with white noise (Lower) }
\label{fig3}
\end{center}
\end{figure}
Elad Den
  • 3,941
  • you have several line breaks between each tikz picture, this tells LaTeX that each picture is a paragraph. Try deleting the empty lines between the pictures and see what happens – Elad Den Mar 20 '17 at 07:37
  • The code is not a MWE. We need your preamble and the data(.txt). So begin with \begin{documentclass}... and end with \endocument}. See: https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that. Also a good start tour you can find here: http://tex.stackexchange.com/tour. Then we can help you better and faster... To your question its possible a duplicate to this http://tex.stackexchange.com/q/213075/124842 – Bobyandbob Mar 20 '17 at 07:48

2 Answers2

2

Gernot already explained the "problem" of your attempt in all detail in his answer, so I will not repeat it here.

Instead I present two possible solutions that result in a bit more "good looking" results (I hope), using Gernot's code as a basis. For more details on how it works, please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}
    % load necessary libraries
    \usetikzlibrary{
        calc,
        pgfplots.groupplots,
    }
    \pgfplotsset{
        % use this `compat' level or higher to use the advanced positioning of
        % the axis labels
        compat=1.3,
    }
    % define a cycle list that can then be used later on
    \pgfplotscreateplotcyclelist{my cycle list}{
        red,\\
        magenta,\\
        cyan,\\
        blue,\\
        gray,\\
        darkgray\\
    }
\begin{document}

% here I present a solution that still has the legend on the right side of the plot
\begin{figure}[!hbt]
        \centering
    \begin{tikzpicture}
        % because both axis environments have the same style this can be realized
        % much easier using the `groupplot' environment
        \begin{groupplot}[
            group style={
                % the size of the `groupplot' shall be 2 columns and 1 row ...
                group size=2 by 1,
                % ... with a reduced horizontal separation ...
                horizontal sep=5mm,
                % ... and the y ticklabels and y axis labels should only be
                % showng on the left-most plot
                y descriptions at=edge left,
            },
            % -----------------------------------------------------------------
            % list here all options that are common for both plots
            % -----------------------------------------------------------------
            % adapt the `width' so the whole `groupplot' still fits into `\textwidth'
            width=0.45\textwidth,
            % (I recommend using this style to show units)
            xlabel={$t$ / s},
            ymin=-25,ymax=25,
            xmin=0,xmax=6,
            minor y tick num=1,
            samples=3,
            % use the above defined cycle list here
            cycle list name=my cycle list,
            % also these two options are the same for all `\addplot' commands,
            % so we can state it here
            /tikz/smooth,
            /tikz/mark size=0.8,
        ]
        % this commands starts the first plot
        % to which we give the options, that should be appended only to this one
        \nextgroupplot[
            title={A Sinusoidal Sequence},
        ]
            % because we have used the defined cycle list, this simplifies a lot ...
            \addplot            {x};
            \addplot            {2*x};
            \addplot            {3*x};
            % ... only here one thing is special, so we append this option by
            % using `\addplot+'
            % (of course we could also have included this option in the
            %  definition of the cycle list itself)
            \addplot+ [mark=*]  {-x};
            \addplot            {-2*x};
            \addplot            {-3*x};

        % this starts the second plot
        \nextgroupplot[
            title={Corresponding Limb Lengths},
            % the legend should only be shown for this plot, because it is
            % identical to both plots
            % (also here I prefer showing the units in this style, like for
            %  the `xlabel'.
            %  (Strictly speaking it isn't allowed to divide by the unit "degree",
            %   but I think this is the lesser evil compared to your solution))
            legend entries={
                $\Delta{x}$ / mm,
                $\Delta{y}$ / mm,
                $\Delta{z}$ / mm,
                $\Delta{\psi}$ / °,
                $\Delta{\theta}$ / °,
                $\Delta{\phi}$ / °,
            },
            legend pos=outer north east,
            % because this looks better for these legend entries, use another
            % alignment for them
            legend cell align=left,
        ]
            \addplot            {x^2};
            \addplot            {2*x^2};
            \addplot            {3*x^2};
            \addplot+ [mark=*]  {-x^2};
            \addplot            {-2*x^2};
            \addplot            {-3*x^2};
        \end{groupplot}
    \end{tikzpicture}
    \caption{A sequence of sinusoidal Cartesian trajectories (left),
             and their corresponding limb lengths with white noise (right)}
        \label{fig1}
\end{figure}

% to enlarge the plots even more, here I present a solution that still shows
% the legend centered below both plots
\begin{figure}[!hbt]
        \centering
    \begin{tikzpicture}
        \begin{groupplot}[
            group style={
                group size=2 by 1,
                horizontal sep=5mm,
                y descriptions at=edge left,
            },
            scale only axis,
            width=0.43\textwidth,   % <-- as you can see, the value was increased
            xlabel={$t$ / s},
            ymin=-25,ymax=25,
            xmin=0,xmax=6,
            minor y tick num=1,
            cycle list name=my cycle list,
            samples=3,
            /tikz/smooth,
            /tikz/mark size=0.8,
        ]
        \nextgroupplot[
            title={A Sinusoidal Sequence},
        ]
            \addplot            {x};
            \addplot            {2*x};
            \addplot            {3*x};
            \addplot+ [mark=*]  {-x};
            \addplot            {-2*x};
            \addplot            {-3*x};

        \nextgroupplot[
            title={Corresponding Limb Lengths},
            legend entries={
                $\Delta{x}$ / mm,
                $\Delta{y}$ / mm,
                $\Delta{z}$ / mm,
                $\Delta{\psi}$ / °,
                $\Delta{\theta}$ / °,
                $\Delta{\phi}$ / °,
            },
            legend cell align=left,
            % -----------------------------------------------------------------
            % (adapted from <https://tex.stackexchange.com/a/80223/95441>)
            % -----
            % reorganize the legend so it shows up the entries in 3 columns
            legend columns=3,
            % because then the space between the text and the next symbol/line
            % is very small, I enlarge it a bit
            legend style={
                % (the /tikz/ prefix is necessary here...
                % otherwise, it might end-up with `/pgfplots/every even column`
                % which is not what we want. compare pgfmanual.pdf)
                % -----
                % it is only every "even" column, because essentially the legend
                % isn't drawn in 3 columns as stated above, but in 2*3 columns.
                % In every odd column the symbol/line is drawn and in every
                % even column the corresponding text/label
                /tikz/every even column/.style={
                    column sep=10pt,
                },
            },
            % and the legend should be stored in a label with the given name.
            % That allows to reference it whereever we want.
            legend to name={plot:legend},
            % -----------------------------------------------------------------
        ]
            \addplot            {x^2};
            \addplot            {2*x^2};
            \addplot            {3*x^2};
            \addplot+ [mark=*]  {-x^2};
            \addplot            {-2*x^2};
            \addplot            {-3*x^2};
        \end{groupplot}

        % Because we want to center it below the plots, we first finished them
        % and now we place a node below it at the given coordinate and as text
        % we simply need to reference the labeled/stored legend
        \node [
            % use this anchor for the node
            anchor=north,
        % this calculates the middle between the both plots without taking the
        % ticklabels into account from the left plot
        % (the `groupplot' environment names the environment by default with the
        %  name `group' and each plot gets an appended name.)
        ] at ($(group c1r1.outer south west)!0.5!(group c2r1.outer south east)$)
                {\ref{plot:legend}};
    \end{tikzpicture}
    \caption{A sequence of sinusoidal Cartesian trajectories (left),
             and their corresponding limb lengths with white noise (right)}
        \label{fig2}
\end{figure}
\end{document}

image showing the zoomed result of above code

Stefan Pinnow
  • 29,535
1

The main problem is that each of your graphs already fills more than half of the text width. Here is your graph in the article class, with the black frame marking the border of the text area. (My graphs uses linear functions since you did not supply the data files.)

enter image description here

So if you want to have the graphs side by side, you have to scale them down, using e.g. the option width=0.3\textwidth. (The best value depends on your actual text width.)

enter image description here

(The black frames are generated by the package showframe and are just meant for demonstration purposes; they also show the head and margin areas.)

\documentclass{article}
%\usepackage{showframe}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{figure}[!hbt]
\begin{center}

\begin{tikzpicture}
\begin{axis}[width=0.3\textwidth,
title={A Sinusoidal Sequence},
xlabel={$t[\rm{sec}]$},
ymin=-25,ymax=25,
xmin=0,xmax=6,
minor y tick num=1,
samples=3,
legend entries={$\Delta{x}[\rm{mm}]$,$\Delta{y}[\rm{mm}]$,$\Delta{z}[\rm{mm}]$,$\Delta{\psi}[^{\circ}]$,$\Delta{\theta}[^{\circ}]$,$\Delta{\phi}[^{\circ}]$},
legend pos=outer north east
]
\addplot[mark size=0.8,draw=red,smooth] plot {x};
\addplot[mark size=0.8,draw=magenta,smooth] plot {2*x};
\addplot[mark size=0.8,draw=cyan,smooth] plot {3*x};
\addplot[mark=*,mark size=0.8,draw=blue,smooth] plot {-x};
\addplot[mark size=0.8,draw=gray] plot {-2*x};
\addplot[mark size=0.8,draw=darkgray] plot {-3*x};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[width=0.3\textwidth,
title={Corresponding Limb Lengths},
xlabel={$t[\rm{sec}]$},
ymin=-25,ymax=25,
xmin=0,xmax=6,
minor y tick num=1,
samples=3,
legend entries={$\Delta{x}[\rm{mm}]$,$\Delta{y}[\rm{mm}]$,$\Delta{z}[\rm{mm}]$,$\Delta{\psi}[^{\circ}]$,$\Delta{\theta}[^{\circ}]$,$\Delta{\phi}[^{\circ}]$},
legend pos=outer north east
]
\addplot[mark size=0.8,draw=red,smooth] plot {x^2};
\addplot[mark size=0.8,draw=magenta,smooth] plot {2*x^2};
\addplot[mark size=0.8,draw=cyan,smooth] plot {3*x^2};
\addplot[mark=*,mark size=0.8,draw=blue,smooth] plot {-x^2};
\addplot[mark size=0.8,draw=gray] plot {-2*x^2};
\addplot[mark size=0.8,draw=darkgray] plot {-3*x^2};
\end{axis}
\end{tikzpicture}
\caption{A sequence of sinusoidal Cartesian trajectories (left), and their corresponding limb lengths with white noise (right) }
\label{fig3}
\end{center}
\end{figure}
\end{document}
gernot
  • 49,614