1

I am trying to plot two stacked x bar plots with error bars.

Everything works fine in the left plot, while in the right plot the markers of the error bar are misplaced even though I use the exact same code for the image. Any ideas how this can be avoided? Thank you.

\documentclass{article} 
\usepackage{pgfplots} 
\colorlet{C0}{blue!20} 
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
data/.style={
    inner sep=0pt
    }
]
\def\figwid{.4} 
\def\figsep{.05}
\def\figseph{0.1}

\def\yoneone{.12}
\def\yonetwo{-.12}
\def\ytwoone{-.88}
\def\ytwotwo{-1.12}
\def\ythreeone{-1.88}
\def\ythreetwo{-2.12}

%%%%%%%%%%%%%%%%%%% R2
\node[
    data
] (R2)
{
    \begin{tikzpicture}[
        inner sep=0.333em, 
        outer sep=0.5\pgflinewidth
        ]
        \begin{axis}[
            xbar stacked,
            legend style={
                legend columns = 4,
                at={(.5,-0.35)},anchor=north,
                draw=none,
                font=\footnotesize
                },
            ytick={0,-1,-2},
            draw = none,
            bar width= 3mm,
            axis y line*=none,
            axis x line*=bottom,
            yticklabels = {N1, M, H},
            xlabel={$R_2\quad[\mathrm{s^{-1}}]$},
            width=\figwid\textwidth,
            ]
            \addplot[C0,fill=C0,error bars/.cd, x dir=both, x explicit, error bar style =blue] coordinates
                {(0,\yoneone)
                (44,\yonetwo) += (9,0) -=(9,0)
                (0,\ytwoone) 
                (43,\ytwotwo) += (5,0) -=(5,0)
                (0,\ythreeone)
                (51, \ythreetwo) += (12,0) -=(12,0)
                }; 

        \end{axis}


    \end{tikzpicture}
};

\node[
    data,
    right = \figsep\textwidth of R2.east,
    anchor = west,
] (R2p)
{
    \begin{tikzpicture}[
        inner sep=0.333em, 
        outer sep=0.5\pgflinewidth
        ]
        \begin{axis}[
            xbar stacked,
            legend style={
                legend columns = 4,
                at={(.5,-0.35)},anchor=north,
                draw=none,
                font=\footnotesize
                },
            ytick={0,-1,-2},
            draw = none,
            bar width= 3mm,
            axis y line*=none,
            axis x line*=bottom,
            yticklabels = {N1, M, H},
            xlabel={$R_2\quad[\mathrm{s^{-1}}]$},
            width=\figwid\textwidth,
            ]
            \addplot[C0,fill=C0,error bars/.cd, x dir=both, x explicit, error bar style =blue] coordinates
                {(0,\yoneone)
                (44,\yonetwo) += (9,0) -=(9,0)
                (0,\ytwoone) 
                (43,\ytwotwo) += (5,0) -=(5,0)
                (0,\ythreeone)
                (51, \ythreetwo) += (12,0) -=(12,0)
                };

        \end{axis}


    \end{tikzpicture}
};
\end{tikzpicture}
\end{document}

The x bar plot with misplaced error ticks.

4
  • Thank you! As this is my first post: Is there missing something important?
    – Malte B
    Nov 14, 2017 at 15:11
  • It's always better to have a complete example, including the necessary packages (and libraries and colour definitions etc.), so that your example can be copied and compiled directly, without making changes and assumptions about your setup. In some cases it's absolutely required to figure the problem, and in a case like this it just makes things easier, as we don't have to find out what is missing. (But don't add an entire mile-long preamble, for this case \documentclass{article} \usepackage{pgfplots} \colorlet{C0}{blue!20} \usetikzlibrary{positioning} is enough.) Nov 14, 2017 at 15:50
  • General tip: don't put tikzpictures inside nodes. (That's the cause of your problem here, and it's completely unnecessary to do it.) Nov 14, 2017 at 15:57
  • Ok, that's probably true, but just deleting the tikzpicutre environment causes errors. As google'ing just reproduced the \node\tikzpicture structure, can you give me a hint or a buzzword how I can align two plots as they are right now?
    – Malte B
    Nov 14, 2017 at 16:02

1 Answer 1

2

Nesting tikzpictures will often lead to problems, as it does here. But it's also not at all necessary to do it here. Place the second axis by using the at key, for example as in the code below. You could also look at the groupplots library.

output of code

\documentclass{article}
\usepackage{pgfplots}
\colorlet{C0}{blue!30}
\begin{document}
\begin{tikzpicture}[
data/.style={
    inner sep=0pt
    }
]
\def\figwid{.4} 
\def\figsep{.05}
\def\figseph{0.1}

\def\yoneone{.12}
\def\yonetwo{-.12}
\def\ytwoone{-.88}
\def\ytwotwo{-1.12}
\def\ythreeone{-1.88}
\def\ythreetwo{-2.12}

        \begin{axis}[
            name=ax1,
            xbar stacked,
            legend style={
                legend columns = 4,
                at={(.5,-0.35)},anchor=north,
                draw=none,
                font=\footnotesize
                },
            ytick={0,-1,-2},
            draw = none,
            bar width= 3mm,
            axis y line*=none,
            axis x line*=bottom,
            yticklabels = {N1, M, H},
            xlabel={$R_2\quad[\mathrm{s^{-1}}]$},
            width=\figwid\textwidth,
            ]
            \addplot[C0,fill=C0,error bars/.cd, x dir=both, x explicit, error bar style =blue] coordinates
                {(0,\yoneone)
                (44,\yonetwo) += (9,0) -=(9,0)
                (0,\ytwoone) 
                (43,\ytwotwo) += (5,0) -=(5,0)
                (0,\ythreeone)
                (51, \ythreetwo) += (12,0) -=(12,0)
                }; 

        \end{axis}

        \begin{axis}[
            at={([xshift=\figsep cm]ax1.outer east)},anchor=outer west,
            xbar stacked,
            legend style={
                legend columns = 4,
                at={(.5,-0.35)},anchor=north,
                draw=none,
                font=\footnotesize
                },
            ytick={0,-1,-2},
            draw = none,
            bar width= 3mm,
            axis y line*=none,
            axis x line*=bottom,
            yticklabels = {N1, M, H},
            xlabel={$R_2\quad[\mathrm{s^{-1}}]$},
            width=\figwid\textwidth,
            ]
            \addplot[C0,fill=C0,error bars/.cd, x dir=both, x explicit, error bar style =blue] coordinates
                {(0,\yoneone)
                (44,\yonetwo) += (9,0) -=(9,0)
                (0,\ytwoone) 
                (43,\ytwotwo) += (5,0) -=(5,0)
                (0,\ythreeone)
                (51, \ythreetwo) += (12,0) -=(12,0)
                };

        \end{axis}


    \end{tikzpicture}
\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .