条形图 tikzpicture 中的条形未完全显示/截断

条形图 tikzpicture 中的条形未完全显示/截断

我认为这是一个简单的修复,但我现在没有看到它。

我有四组数据集 (A、B、C、D),但是在 x = 200 和 x = 800 处条形图未完全显示。

在此先感谢您的帮助。

在此处输入图片描述


\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{external}

\begin{document}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\pgfplotstableread{
    x       y       y+
    200     0.75    0.02
    400     0.76    0.01
    600     0.77    0.01
    800     0.77    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
}{\mytable}


\begin{tikzpicture}[baseline = (current bounding box.north)]
    \begin{axis}[
        ybar,
        ymax=0.85,
        xlabel={-},        
        ylabel={-},
        xtick=data,
        xticklabels = {
            200,400,600,800
        },
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        legend style={anchor=north east},
        ]

        \addplot [ybar, fill=black!60] 
        plot [error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={0}{3}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [ybar, fill=black!40] 
        plot [error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={4}{7}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [ybar, fill=black!20] 
        plot [error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={8}{11},y error plus=y+, y error minus=y+]{\mytable};

        \addplot [ybar, fill=black!10] 
        plot [error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={12}{15},y error plus=y+, y error minus=y+]{\mytable};

    \legend{A,B,C,D}
    \end{axis}

\end{tikzpicture}


\end{document}

回答后更新

谢谢Torbjørn T.,您的解决方案效果很好。感谢您指出我混淆了 pgfplot 和 TikZ 语法。

我对 pgfplot 和 TikZ 语法进行了一些基本且有用的研究这里(也许我应该早点这么做)。

添加更多栏目

也许这对其他人有用。

最后我想总共有 6 个条形图,但图表的宽度不会自动更新。因此我在轴选项中添加了以下内容:scale only axiswidth=0.95\textwidth height=0.3\textwidth

具有 6 个条形和自定义宽度的图表


\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{external}

\begin{document}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\pgfplotstableread{
    x       y       y+
    200     0.75    0.02
    400     0.76    0.01
    600     0.77    0.01
    800     0.77    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.7    0.02
    400     0.7    0.01
    600     0.7    0.01
    800     0.7    0.01
    200     0.6    0.02
    400     0.6    0.01
    600     0.6    0.01
    800     0.6    0.01
}{\mytable}


\begin{tikzpicture}[baseline = (current bounding box.north)]
    \begin{axis}[
        scale only axis, 
        width=0.95\textwidth,
        height=0.3\textwidth,
        ybar,
        ymax=0.85,
        xlabel={-},        
        ylabel={-},
        xtick=data,
        xticklabels = {
            200,400,600,800
        },
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        legend style={
                at={(0.5,-0.4)}, 
                anchor=north, 
                legend columns=-1},
        enlarge x limits={abs=100},
        ]

        \addplot [fill=black!60,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={0}{3}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!40,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={4}{7}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!20,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={8}{11},y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!10,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={12}{15},y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=green!10,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={16}{19},y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=blue!10,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={20}{23},y error plus=y+, y error minus=y+]{\mytable};

    \legend{A,B,C,D,E,F}
    \end{axis}

\end{tikzpicture}

\end{document}

答案1

添加enlarge x limits={abs={100}}axis选项中。

无关紧要的\addplot plot是混合pgfplots和 TikZ 语法,您只需要\addplot [<all the options>]。最后,当您在选项ybar中时axis,每个中都不需要它\addplot

在此处输入图片描述

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{external}

\begin{document}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\pgfplotstableread{
    x       y       y+
    200     0.75    0.02
    400     0.76    0.01
    600     0.77    0.01
    800     0.77    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
    200     0.75    0.02
    400     0.75    0.01
    600     0.76    0.01
    800     0.76    0.01
}{\mytable}


\begin{tikzpicture}[baseline = (current bounding box.north)]
    \begin{axis}[
        ybar,
        ymax=0.85,
        xlabel={-},        
        ylabel={-},
        xtick=data,
        xticklabels = {
            200,400,600,800
        },
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        legend style={anchor=north east},
        enlarge x limits={abs=100}
        ]

        \addplot [fill=black!60,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={0}{3}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!40,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={4}{7}, y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!20,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={8}{11},y error plus=y+, y error minus=y+]{\mytable};

        \addplot [fill=black!10,error bars/.cd, y dir=both, y explicit]
        table[x index=0, y index=1, select coords between index={12}{15},y error plus=y+, y error minus=y+]{\mytable};

    \legend{A,B,C,D}
    \end{axis}

\end{tikzpicture}

\end{document}

相关内容