如何对齐 PGFplot 子图

如何对齐 PGFplot 子图

我正在使用 matlab2tikz MATLAB 函数将带有子图的 MATLAB 图形导出到 tikz 代码。不幸的是,子图没有正确对齐。帖子PGFplot 占据整个 \linewidth,并与 tikzpicture 上的 y=轴对齐似乎解决了类似的问题。但是,我查看了 .tikz 文件中的代码,它似乎使用了该帖子答案中建议的方法,但图表仍然没有正确对齐。下面是一张显示此问题的图像和相应的代码。抱歉,它有点长,这是我能做的最简洁的,但仍然重现了这个问题。

我怎样才能使图表正确对齐?

提前致谢!

图形未正确对齐

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize[prefix=compiled_figures/]
\pgfplotsset{try min ticks=3}
\pgfplotsset{max space between ticks=50pt}



\begin{document}
\begin{figure}[!htbp]
    \centering
    \newlength\figureheight 
    \newlength\figurewidth 
    \setlength\figureheight{3cm} 
    \setlength\figurewidth{2cm}
    \tikzset{external/remake next}

        \begin{tikzpicture}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=0, xmax=3,
        ymin=-0.5, ymax=1.5,
        name=plot3,
        title={$p = 2$}]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (0,0.46)(0.25,0.80)(0.5,1.02)(0.75,1.27)(1,0.59)(1.25,0.15)(1.5,-0.02)(1.75,-0.27)(2,-0.05)(2.25,0.03)(2.5,0)(2.75,0)(3,0) 
        };
        \end{axis}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=0, xmax=1.5,
        ymin=0, ymax=1,
        ylabel={$\phi(t)$},
        name=plot1,
        at=(plot3.left of south west), anchor=right of south east,
        title={$p = 1$}]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (0,1)(0.25,1)(0.5,1)(0.75,1)(1,0)(1.25,0)(1.5,0) 
        };
        \end{axis}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=0, xmax=1,
        ymin=-1, ymax=1,
        ylabel={$\psi(t)$},
        name=plot2,
        at=(plot1.below south west), anchor=above north west]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (0,1)(0.25,1)(0.5,-1)(0.75,-1)(1,0) 
        };
        \end{axis}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=-1, xmax=1.5,
        ymin=-2, ymax=2,
        name=plot4,
        at=(plot2.right of south east), anchor=left of south west]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0) 
        };
        \end{axis}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=-2, xmax=2,
        ymin=-1, ymax=1.5,
        name=plot6,
        at=(plot4.right of south east), anchor=left of south west]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (-2,0.023)(-1.75,0.056)(-1.5,0.089)(-1.25,0.128)(-1,-0.017)(-0.75,-0.238)(-0.5,-0.444)(-0.25,-0.699)(0,0.136)(0.25,1.416)(0.5,0.599)(0.75,-0.787)(1,-0.443)(1.25,0.146)(1.5,0.056)(1.75,-0.0234374999975261)(2,0) 
        };
        \end{axis}

        \begin{axis}[%
        width=\figurewidth,
        height=\figureheight,
        scale only axis,
        xmin=0, xmax=4.5,
        ymin=-0.5, ymax=1.5,
        at=(plot6.above north west), anchor=below south west,
        title={$p = 3$}]
        \addplot [
        color=blue,
        solid,
        line width=2.0pt,
        forget plot
        ]
        coordinates{
         (0,0.221)(0.25,0.536)(0.5,0.842)(0.75,1.212)(1,0.991)(1.25,0.547)(1.5,0.19)(1.75,-0.285)(2,-0.259)(2.25,-0.069)(2.5,-0.032)(2.75,0.070)(3,0.046)(3.25,-0.015)(3.5,-0.006)(3.75,0.002)(4,0)(4.25,0)(4.5,0) 
        };
        \end{axis}
        \end{tikzpicture}%

\end{figure}
\end{document}

答案1

Groupplot 似乎达成了这笔交易。正如你在这里看到的:如何堆叠和对齐具有相同 x 但不同 y 轴的多个图?请注意,您应该调整垂直设置参数以便有一定的间隙以及组大小。

这将是这样的:

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my plots,
        group size=3 by 3,
        ylabels at=edge left
    },
    footnotesize,
    width=5cm,
    height=4cm,
    tickpos=left,
    ytick align=outside,
    xtick align=outside,
    enlarge x limits=false 
]

\nextgroupplot[title={$p=1$},ylabel=$\phi(t)$]
\addplot[solid] coordinates{
         (0,1)(0.25,1)(0.5,1)(0.75,1)(1,0)(1.25,0)(1.5,0) 
        };
\nextgroupplot[title={$p=2$}]
\addplot[solid] coordinates{
         (0,1)(0.25,1)(0.5,-1)(0.75,-1)(1,0) 
        };
\nextgroupplot[title={$p=3$}]
\addplot [solid]
        coordinates{
         (-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0) 
        };
\nextgroupplot[ylabel=$\psi(t)$]
\addplot [solid]
        coordinates{
         (-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0) 
        };
\nextgroupplot
\addplot [solid]
        coordinates{
         (-2,0.023)(-1.75,0.056)(-1.5,0.089)(-1.25,0.128)(-1,-0.017)(-0.75,-0.238)(-0.5,-0.444)(-0.25,-0.699)(0,0.136)(0.25,1.416)(0.5,0.599)(0.75,-0.787)(1,-0.443)(1.25,0.146)(1.5,0.056)(1.75,-0.0234374999975261)(2,0) 
        };
\nextgroupplot
\addplot [solid]
        coordinates{
         (0,0.221)(0.25,0.536)(0.5,0.842)(0.75,1.212)(1,0.991)(1.25,0.547)(1.5,0.19)(1.75,-0.285)(2,-0.259)(2.25,-0.069)(2.5,-0.032)(2.75,0.070)(3,0.046)(3.25,-0.015)(3.5,-0.006)(3.75,0.002)(4,0)(4.25,0)(4.5,0) 
        };
\end{groupplot}
\end{tikzpicture}

\end{document}

结果

当然,您也可以添加您的情节选项(我只保留了实体以缩短代码)...

相关内容