错误刻度在堆叠的 xbar 图 pgfplots 中放错了位置

错误刻度在堆叠的 xbar 图 pgfplots 中放错了位置

我正在尝试绘制两个带有误差线的堆叠 x 条形图。

左图中一切正常,而右图中误差线的标记位置错误,尽管我对图像使用了完全相同的代码。有什么办法可以避免这种情况吗?谢谢。

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

带有错误位置错误刻度的 x 条形图。

答案1

嵌套tikzpictures 通常会导致问题,就像这里一样。但这里也完全没有必要这样做。axis 使用at键放置第二个,例如如下面的代码所示。您也可以查看groupplots库。

代码输出

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

相关内容