x 刻度位于间隔之间,而不是在条形下方

x 刻度位于间隔之间,而不是在条形下方

我想制作一个堆叠图,其中 xticks 显示样本数量。但我的 xticks 介于间隔之间,无论我怎么尝试都无法修复。更奇怪的是,我有 21 个样本,但图表只显示 20 个作为最后一个。这是我的代码:

\begin{figure}[bh]
\begin{tikzpicture} 
        \begin{axis}[
            ybar stacked,
            xtick = data,
            xmin=1, xmax=21,
            %xtick = {1,...,22},
            %x tick label as interval = false,
            %xticklabels = {1,...,22},              
            %xtick distance = 2
            %xticklabels={1,, ,, ,, ,, ,,10,, ,, ,, ,, ,,21,,},
            %xtick distance = 1,
            %xlabel near ticks,
            enlargelimits=true,
            ybar interval=0.05,
            bar width = 0.14cm
            ]

\addplot[ybar,fill=red] 
coordinates {(1,30)
        (2,30)
        (3,30)
        (4,80)
        (5,50)
        (6,40)
        (7,90)
        (8,30)
        (9,80)
        (10,50)
        (11,85)
        (12,75)
        (13,35)
        (14,50)
        (15,40)
        (16,50)
        (17,80)
        (18,30)
        (19,10)
        (20,80)
        (21,80)

        };

        \addplot[ybar,fill=green] 
coordinates {(1,52)
        (2,61)
        (3,53)
        (4,54)
        (5,62)
        (6,64)
        (7,57)
        (8,45)
        (9,45)
        (10,46)
        (11,54)
        (12,57)
        (13,52)
        (14,63)
        (15,50)
        (16,51)
        (17,47)
        (18,57)
        (19,33)
        (20,30)
        (21,58)

        };
\end{axis}
\end{tikzpicture}    
\end{figure}

在此处输入图片描述

答案1

像这样?

在此处输入图片描述

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
        \begin{axis}[width=12cm,
            ybar stacked,
            bar width = 3mm,
            xtick = {1,...,21},
            xticklabels = {1,...,21},
            x tick label style = {font=\scriptsize},
            enlargelimits=0.05,
            ]

\addplot[ybar,fill=red]
coordinates {(1,30) (2,30)  (3,30)  (4,80)  (5,50)
             (6,40) (7,90)  (8,30)  (9,80)  (10,50)
             (11,85) (12,75) (13,35) (14,50) (15,40)
             (16,50) (17,80) (18,30) (19,10) (20,80)
             (21,80)
            };

        \addplot[ybar,fill=green]
coordinates {(1,52) (2,61)  (3,53)  (4,54)  (5,62)
             (6,64) (7,57)  (8,45)  (9,45)  (10,46)
             (11,54)(12,57) (13,52) (14,63) (15,50)
             (16,51)(17,47) (18,57) (19,33) (20,30)
             (21,58)
            };
\end{axis}
\end{tikzpicture}
\end{document}

相关内容