奇怪的 pgfplots 图表和正文重叠 - 如何调整图表外边距?

奇怪的 pgfplots 图表和正文重叠 - 如何调整图表外边距?

正如标题所示,我的 pgfplots 图表与节后的任何文本重叠{tikzpicture}。我尝试了常规文本,如下面的示例所示,并将图包裹起来,{figure}结果相同(在这种情况下,图表与图标题重叠)。

有人知道我应该在这里做什么来“推开”后面的内容吗{tikzpicture}?我尝试在 pgfplots 手册中寻找答案,然后再在这里提问。不管怎样,图表看起来正是我想要的样子。

代码如下:

    \usepackage{pgfplots}
    \pgfplotsset{compat=1.5}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[
        title=a),
        ymin=0,
        ymax=10,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},
        xtick=data,
        width=.5\columnwidth,
        /pgf/bar width=20pt, % bar width
        name=main plot,
        ylabel=Some y axis label,
        ]
        \addplot+ [
            ybar,
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,4) +- (0,.1)
        (B,3) +- (0,.2)
        (C,6) +- (0,.15)
        (D,5) +- (0,.3)
        (E,8) +- (0,.175)
        (F,7) +- (0,.25)
        };
    \end{axis}
    
    \pgfplotsset{set layers}
    \begin{axis}[
        title=b),
        ymin=0,
        ymax=10,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},
        xtick=data,
        width=.5\columnwidth,
        /pgf/bar width=20pt, % bar width
        at={(main plot.below south west)}, yshift=-1cm, anchor=north west,
        axis y line*=left, % the '*' avoids arrow heads
        ylabel={Adhesion Force, nN},
        ]
        \addplot+ [
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,2) +- (0,.1)
        (B,9) +- (0,.2)
        (C,7) +- (0,.1)
        (D,2) +- (0,.2)
        (E,1) +- (0,.3)
        (F,4) +- (0,.4)
        };
    \end{axis}
    \begin{axis}[
        red,
        y axis line style={red},
        y tick style={red},
        ymin=0,
        ymax=11,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},    
        xtick=data,
        width=.5\columnwidth,
        at={(main plot.below south west)}, yshift=-1cm, anchor=north west,
        axis y line*=right,
        axis x line=none,
        ylabel=Friction Coefficient,
        ]
        \addplot+ [
            red,
            mark=o,
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,7) +- (0,.5)
        (B,2) +- (0,.7)
        (C,3) +- (0,.2)
        (D,6) +- (0,.9)
        (E,8) +- (0,.2)
        (F,5) +- (0,.3)
        };  
    \end{axis}
    \end{tikzpicture}
    Then there is some text here that says nothing but is clashing with the charts.

\end{document}

它在编辑器中的样子如下:

在此处输入图片描述

显然,我希望文本从下图及其 x 标签开始,就像任何其他图形一样。

答案1

欢迎!您应该避免在轴选项中进行这些额外的移动。相反,移动锚点坐标(并使用较新的版本并开始新的一行)。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

    \begin{tikzpicture}
    \begin{axis}[
        title=a),
        ymin=0,
        ymax=10,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},
        xtick=data,
        width=.5\columnwidth,
        /pgf/bar width=20pt, % bar width
        name=main plot,
        ylabel=Some y axis label,
        ]
        \addplot+ [
            ybar,
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,4) +- (0,.1)
        (B,3) +- (0,.2)
        (C,6) +- (0,.15)
        (D,5) +- (0,.3)
        (E,8) +- (0,.175)
        (F,7) +- (0,.25)
        };
    \end{axis}
    \path ([yshift=-1cm]main plot.below south west) coordinate (tl);
    \pgfplotsset{set layers}
    \begin{axis}[
        title=b),
        ymin=0,
        ymax=10,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},
        xtick=data,
        width=.5\columnwidth,
        /pgf/bar width=20pt, % bar width
        at={(tl)},  anchor=north west,
        axis y line*=left, % the '*' avoids arrow heads
        ylabel={Adhesion Force, nN},
        ]
        \addplot+ [
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,2) +- (0,.1)
        (B,9) +- (0,.2)
        (C,7) +- (0,.1)
        (D,2) +- (0,.2)
        (E,1) +- (0,.3)
        (F,4) +- (0,.4)
        };
    \end{axis}
    \begin{axis}[
        red,
        y axis line style={red},
        y tick style={red},
        ymin=0,
        ymax=11,
        scaled y ticks = false,
        symbolic x coords={A,B,C,D,E,F},    
        xtick=data,
        width=.5\columnwidth,
        at={(tl)},
        anchor=north west,
        axis y line*=right,
        axis x line=none,
        ylabel=Friction Coefficient,
        ]
        \addplot+ [
            red,
            mark=o,
            error bars/.cd,
                y dir=both, y explicit,
                x dir=both, x explicit,
        ] coordinates {
        (A,7) +- (0,.5)
        (B,2) +- (0,.7)
        (C,3) +- (0,.2)
        (D,6) +- (0,.9)
        (E,8) +- (0,.2)
        (F,5) +- (0,.3)
        };  
    \end{axis}
    \end{tikzpicture}

    Then there is some text here that says nothing but is clashing with the charts.

\end{document}

在此处输入图片描述

PS 通常人们会把这样的情节放在figure环境中。

相关内容