我怎样才能将计算出的两张 pfgplots 图表放在一行中?

我怎样才能将计算出的两张 pfgplots 图表放在一行中?

我正在尝试制作一条水平线上的两个图形,但找不到合适的方法将它们放在一行中在此处输入图片描述 谁来帮帮我??

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

答案1

Gernot 已经详细解释了你的尝试的“问题”他的回答,因此这里就不再重复了。

相反,我使用 Gernot 的代码作为基础,提出了两种可能的解决方案,它们会产生更“好看”的结果(我希望如此)。有关其工作原理的更多详细信息,请查看代码中的注释。

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

该图显示了上述代码的放大结果

答案2

主要问题是,您的每张图表已经占满了文本宽度的一半以上。这是文章类中的图表,黑色框架标记了文本区域的边框。(由于您没有提供数据文件,因此我的图表使用线性函数。)

在此处输入图片描述

因此,如果要将图表并排显示,则必须缩小它们,例如使用选项width=0.3\textwidth。(最佳值取决于您的实际文本宽度。)

在此处输入图片描述

(黑色框架由包生成showframe,仅用于演示目的;它们还显示头部和边缘区域。)

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

相关内容