TransWikia.com

Strange pgfplots chart and body text overlap - how to adjust plot outer margin?

TeX - LaTeX Asked on June 30, 2021

As title says, my pgfplots chart is overlapping with any text that follows {tikzpicture} section. I tried regular text, like in the sample below, and also wrapping the plot in {figure} with the same results (in that case chart overlaps with the figure caption).

Does anyone have any idea what I should do here to "push" what follows away from the {tikzpicture}? I tried finding answer in pgfplots manual before asking here. For what it’s worth, charts look just the way I want them.

The code is as follows:

    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=.5columnwidth,
        /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=.5columnwidth,
        /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=.5columnwidth,
        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}

And this is how it looks like in the editor:

enter image description here

Obviously, I would like for text to start under the lower chart and its x label, just as it would for any other figure.

One Answer

Welcome! You should avoid these additional shifts in the options of the axis. Rather, shift the anchor coordinate (and use a newer version and start a new line).

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=.5columnwidth,
        /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=.5columnwidth,
        /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=.5columnwidth,
        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}

enter image description here

P.S. Normally one puts such plots in a figure environment.

Answered by user194703 on June 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP