应堆叠的条形图发生移动且未对齐

应堆叠的条形图发生移动且未对齐

我正在尝试创建一个堆积条形图,但是当我绘制三组数据点时,它们没有正确对齐/移动。

\documentclass{article}

\usepackage{pgfplots}


\begin{document}


\begin{tikzpicture}
\begin{axis}[
title=Number 1985-2021,
bar width=3pt,
ybar=5pt,
%ybar=-1cm, % because of bar width=1cm
    x tick label style={
        /pgf/number format/1000 sep=},
       % axis lines=left,
         enlarge y limits=0.01,
         enlarge x limits=0.1,
%enlarge x limits={abs=2},
xlabel={Year},
ybar stacked, ymin=0, ymax=380,
xlabel style={yshift=-4ex},
        nodes near coords align={vertical},
xtick={1985,1990,1995,2000,2005,2010,2015,2020},
ytick={0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400},
xticklabel style={rotate=90},
xticklabel style = {yshift=0.5ex},
]

\addplot [fill=black] coordinates {(1985,0)(1986,0)(1987,0)(1988,0)(1989,0)(1990,40)(1991,47)(1992,57)(1993,59)(1994,58)(1995,88)(1996,77)(1997,67)(1998,69)(1999,92)(2000,78)(2001,75)(2002,86)(2003,85)(2004,58)(2005,60)(2006,50)(2007,76)(2008,64)(2009,80)(2010,76)(2011,58)(2012,50)(2013,56)(2014,37)(2015,49)(2016,49)(2017,55)(2018,69)(2019,91)(2020,80)(2021,84)};

\addplot [fill=red] coordinates {(1985,86)(1986,219) (1987,167)(1988,172)(1989,215)(1990,182)(1991,201)(1992,228)(1993,235)(1994,210)(1995,192)(1996,203)(1997,212)(1998,208)(1999,228)(2000,290)(2001,217)(2002,176)(2003,188)(2004,170)(2005,205)(2006,179)(2007,188)(2008,168)(2009,147)(2010,148)(2011,151)(2012,101)(2013,138)(2014,137)(2015,97)(2016,71)(2017,128)(2018,158)(2019,150)(2020,142)(2021,27)};

\addplot [fill=green] coordinates {(1985,0)(1986,0)(1987,0)(1988,0)(1989,0)(1990,0)(1991,0)(1992,0)(1993,0)(1994,0)(1995,0)(1996,0)(1997,0)(1998,0)(1999,0)(2000,0)(2001,0)(2002,0)(2003,0)(2004,0)(2005,0)(2006,0)(2007,0)(2008,0)(2009,0)(2010,0)(2011,68)(2012,85)(2013,58)(2014,71)(2015,103)(2016,78)(2017,56)(2018,19)(2019,25)(2020,46)(2021,33)};

\end{axis}
\end{tikzpicture}

\end{document}

错位图片

答案1

  • 如果你在 MWE 中删除ybar=5pt图表,将得到正确的结果。你也可以在答案中发现这一点。
  • ybar=<width>在非堆积条形图中有意义,它定义给定坐标周围的条形之间的距离。
  • 图表的宽度有点太窄,没有足够的空间容纳所有条形图,因此它们重叠,但是它不应该比\textwidth(您在答案中设置的)更宽。

您的图表的一个可能的解决方案是,在表格中收集条形图的数据,并稍微改变刻度标签的样式。

编辑: 删除了未使用的库和包(unitssiunitx)以及 filecontents现已成为 LaTeX 内核一部分的过时包。

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % recent version

\begin{filecontents}{xdata.data}
x       y1      y2      y3
1985    86      0       0
1986    219     0       0
1987    167     0       0
1988    172     0       0
1989    215     0       0
1990    182     40      0 
1991    201     47      0
1992    228     57      0
1993    235     59      0
1994    210     58      0
1995    192     88      0
1996    203     77      0
1997    212     67      0
1998    208     69      0
1999    228     92      0
2000    290     78      0
2001    217     75      0
2002    176     86      0
2003    188     85      0
2004    170     58      0
2005    205     60      0
2006    179     50      0
2007    188     76      0
2008    168     64      0
2009    147     80      0
2010    148     76      0
2011    151     58      68
2012    101     50      85
2013    138     56      58
2014    137     37      71
2015    97      49      103
2016    71      49      78
2017    128     55      56
2018    158     69      19
2019    150     91      25
2020    142     80      46
2021    27      84      33 
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[width=\linewidth,
   title={Numbers},
    ybar stacked,
    xmin=1983,  xmax=2023,  ymin=0, ymax=400,
    bar width=2mm,
x tick label style={/pgf/number format/1000 sep=},
%xtick={1985,1990,...,2020},
ytick={0,100,...,400},
minor tick num=4,
xlabel={Year},
xtick align=outside,
ticklabel style={font=\footnotesize},
legend pos=north east,
            ]
\addplot    table [y=y1] {xdata.data};
\addplot    table [y=y2] {xdata.data};
\addplot    table [y=y3] {xdata.data};

\legend{S1, S2, S3}
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

因此,经过进一步调查,我能够通过改变 addplot 方法来堆叠它们;但是,我仍然不确定第一种方法有什么问题。

    \documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}

\pgfplotsset{compat=1.18} % or whatever version you have
\begin{document}


\begin{tikzpicture}
\begin{axis}[
width=1.3\textwidth,    % set width
height=0.8\textwidth, % and height
   title={Numbers},
    ybar stacked, ymin=0, xmin=1983,xmax=2023,ymax=400,
    bar width=2mm,
x tick label style={
        /pgf/number format/1000 sep=},
xlabel style={yshift=-4ex},
nodes near coords align={vertical},
xtick={1985,1990,1995,2000,2005,2010,2015,2020},
ytick={0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,400},
xlabel={Year},
xtick align=outside,
xticklabel style={rotate=75},
xticklabel style = {yshift=0.4ex},
  ]

    \addplot+[ybar] plot coordinates {(1985,86)(1986,219) (1987,167)(1988,172)(1989,215)(1990,182)(1991,201)(1992,228)(1993,235)(1994,210)(1995,192)(1996,203)(1997,212)(1998,208)(1999,228)(2000,290)(2001,217)(2002,176)(2003,188)(2004,170)(2005,205)(2006,179)(2007,188)(2008,168)(2009,147)(2010,148)(2011,151)(2012,101)(2013,138)(2014,137)(2015,97)(2016,71)(2017,128)(2018,158)(2019,150)(2020,142)(2021,27)};

    \addplot+[ybar] plot coordinates {(1985,0)(1986,0)(1987,0)(1988,0)(1989,0)(1990,40)(1991,47)(1992,57)(1993,59)(1994,58)(1995,88)(1996,77)(1997,67)(1998,69)(1999,92)(2000,78)(2001,75)(2002,86)(2003,85)(2004,58)(2005,60)(2006,50)(2007,76)(2008,64)(2009,80)(2010,76)(2011,58)(2012,50)(2013,56)(2014,37)(2015,49)(2016,49)(2017,55)(2018,69)(2019,91)(2020,80)(2021,84)};

    \addplot+[ybar] plot coordinates {(1985,0)(1986,0)(1987,0)(1988,0)(1989,0)(1990,0)(1991,0)(1992,0)(1993,0)(1994,0)(1995,0)(1996,0)(1997,0)(1998,0)(1999,0)(2000,0)(2001,0)(2002,0)(2003,0)(2004,0)(2005,0)(2006,0)(2007,0)(2008,0)(2009,0)(2010,0)(2011,68)(2012,85)(2013,58)(2014,71)(2015,103)(2016,78)(2017,56)(2018,19)(2019,25)(2020,46)(2021,33)};

  \legend{\strut S1,\strut  S2,\strut  S3}
  \end{axis}
\end{tikzpicture}

\end{document}

相关内容