我想要重现的图如下:
我以前能够按照 pgfplots 手册第 4.5.9 章中的示例使用坐标来生成堆叠的 ybar 图。不幸的是,现在我有太多数据点,手动执行此操作需要很长时间。有没有一种简单的方法可以实现类似于上图的效果?我尝试从以前的问题中搜索示例,但不幸的是,我找不到适合我需要的。
所需图表的数据如下:
\pgfplotstableread[col sep=comma,header=true]{
Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data
答案1
目前尚不清楚您认为需要手动执行什么操作,但只需使用ybar stacked
和绘制每个部分就可以了。
对于条形图顶部的总数,我们可以使用\addplot+[nodes near coords]
堆栈中的最后一个图,如下所示堆积条形图:显示总值。然而,在pgfplots
v1.9 及更高版本中,添加了一些额外的代码来修改nodes near coords
堆叠条形图类型的行为。手册中第 4.5.9 节末尾讨论了这一点。
对于大多数条形图来说,这些修改都是有意义的,也是合理的。但这里不需要它们。因此,我们可以通过清空pgfplots
某个时候使用的内部样式来覆盖这些修改前设置ybar stacked
。这通常看起来像
\pgfplotsset{nodes near coords ybar stacked configuration/.style={}}
并且它可以在文档中进行本地或全局设置,具体取决于其影响的广泛程度。
完整代码如下:
\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.12}
\pgfplotstableread[col sep=comma,header=true]{
Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords ybar stacked configuration/.style={}, % disable modifications
ybar stacked,
]
\addplot table[x=Year,y=China] from \data;
\addplot table[x=Year,y=Americas] from \data;
\addplot table[x=Year,y=APAC] from \data;
\addplot+[nodes near coords] table[x=Year,y=Europe] from \data;
\end{axis}
\end{tikzpicture}
\end{document}
当然,轴/刻度标签、间距和其他内容需要更改才能达到所需的结果,但所有这些都是可行的。我认为你的问题更侧重于条形图本身的堆叠。
结果如下:
编辑 3:在评论中添加了另一个请求,要求标签中不包含科学符号nodes near coords
。这些节点的样式由样式决定every node near coord
,该样式最初为空。我们可以使用标准数字打印样式禁用科学符号pgf/number format/fixed
。在这里,我还展示了通过添加来减小这些标签的字体大小node font=\scriptsize
:
\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.12}
\pgfplotstableread[col sep=comma,header=true]{
Year,RoW,MEA,China,Americas,APAC,Europe
2000,751,0,19,21,368,129
2001,807,0,24,24,496,265
2002,887,0,42,54,686,399
2003,964,0,52,102,916,601
2004,993,1,62,163,1198,1306
2005,1003,1,70,246,1502,2291
2006,1108,1,80,355,1827,3289
2007,1150,2,100,522,2098,5312
2008,1226,3,140,828,2628,11020
2009,1306,25,300,1328,3373,16854
2010,1590,80,800,2410,4951,30505
2011,2098,205,3300,4590,7513,52764
2012,2098,570,6800,8365,12159,70489
2013,2098,953,18600,13727,21992,81464
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}[
nodes near coords ybar stacked configuration/.style={}, % disable modifications
ybar stacked,
every node near coord/.style={node font=\scriptsize,/pgf/number format/fixed},
]
\addplot table[x=Year,y=China] from \data;
\addplot table[x=Year,y=Americas] from \data;
\addplot table[x=Year,y=APAC] from \data;
\addplot+[nodes near coords] table[x=Year,y=Europe] from \data;
\end{axis}
\end{tikzpicture}
\end{document}