我知道可以只在条形图顶部显示总和并删除堆积条形图中的其他值(如附图所示),但我无法弄清楚我的代码出了什么问题。这是我的 MWE:
\documentclass[a4paper]{report}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{
compat=newest}
\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
},
},
}
\begin{document}
\pgfplotstableread{
Year OneCol SecCol ThirdCol ForthCol FifthCol SixthCol SeventhCol EigthCol NinthCol TenthCol EleventhCol
2005 0.2 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.5 0.0 0.3
2006 0.3 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.5 0.0 0.3
2007 0.5 0.0 0.0 0.0 0.0 0.2 0.0 0.0 -2.1 0.0 0.8
2008 1.2 0.0 0.0 0.0 0.0 0.6 0.0 0.0 -1.8 0.0 1.9
2009 0.5 0.0 0.0 0.0 0.0 0.3 0.0 0.0 -2.1 0.0 0.9
2010 3.9 0.0 0.0 0.0 0.0 1.0 0.0 0.0 -1.4 0.0 5.6
2011 0.6 0.0 0.0 0.0 0.0 0.3 0.0 0.0 -1.9 -0.1 0.8
2012 11.1 0.0 0.0 0.0 0.0 2.0 0.0 0.0 -1.0 0.0 13.7
2013 0.2 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.6 0.0 0.2
}\datatable
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.9*\textwidth,
tick label style={/pgf/number format/1000 sep=,font=\footnotesize},
nodes near coords ybar stacked configuration/.style={},
ybar stacked,
bar width=1em,
ymin=-10,
axis on top,
ymax=30,
height=8cm,
enlarge x limits={true, abs value=0.75},
legend style={
at={(1.1,0.5)},
anchor=west,
draw=none },
restrict y to domain*=:0, % negative values % important workaround!
hide axis % <-- added, you really only need to draw the axis and labels once
]
\addplot table [y index=1] {\datatable};
\addplot table [y index=2] {\datatable};
\addplot table [y index=3] {\datatable};
\addplot table [y index=4] {\datatable};
\addplot table [y index=5] {\datatable};
\addplot table [y index=6] {\datatable};
\addplot table [y index=7] {\datatable};
\addplot table [y index=8] {\datatable};
\addplot table [y index=9] {\datatable};
\addplot table [y index=10] {\datatable};
\addplot table [y index=11] {\datatable};
\end{axis}
\begin{axis}[
width=0.9*\textwidth,
bar width=0.26cm,
tick label style={/pgf/number format/1000 sep=,font=\footnotesize},
ybar stacked,
bar width=1em,
ymin=-10,
axis on top,
ymax=30,
ymajorgrids = true,
enlarge x limits={true, abs value=0.75},
height=8cm,
nodes near coords={
\pgfmathprintnumber[precision=0]{\pgfplotspointmeta}},
legend style={
at={(1.1,0.5)},
anchor=west,
draw=none },
restrict y to domain*=0:, % positive values
xtick=data,
xticklabels={GWP,ODP,POCP, AP, EP(T), EP(FW), EP(M), ADP, CED}
]
\addplot [fill=cyan!70,draw=black!70] table [y index=1] {\datatable};
\addplot [fill=green!90,draw=black!70] table [y index=2] {\datatable};
\addplot [fill=teal!90,draw=black!70]table [y index=3] {\datatable};
\addplot [fill=violet!90,draw=black!70]table [y index=4] {\datatable};
\addplot [fill=lime!90,draw=black!70]table [y index=5] {\datatable};
\addplot [fill=magenta!90,draw=black!70]table [y index=6] {\datatable};
\addplot [fill=red!90,draw=black!70]table [y index=7] {\datatable};
\addplot [fill=blue!90,draw=black!70]table [y index=8] {\datatable};
\addplot [fill=yellow!90,draw=black!70] table [y index=9] {\datatable};
\addplot [fill=purple!90,draw=black!70] table [y index=10] {\datatable};
\addplot [fill=olive!90,draw=black!70,,show sum on top] table [y index=11] {\datatable};
\legend{Facade,Column,Core,Drywall,Foundation,Roof,Slab,Window,Operation,Disposal,Replacement}
\end{axis}
\end{tikzpicture}
\caption{Impacts by category relative to baseline building with conventional building envelope}
\label{relative}
\end{figure}
\end{document}
答案1
第二轴中的以下几行使所有坐标都显示值。
nodes near coords={
\pgfmathprintnumber[precision=0]{\pgfplotspointmeta}},
替换为
nodes near coords={},
抑制它们。实际上,您不需要两个轴分别绘制负数和正数。可以使用stack negative=on previous,
选项来完成。
\documentclass[a4paper]{report}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=newest}
\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
},
},
}
\begin{document}
\pgfplotstableread{
Year OneCol SecCol ThirdCol ForthCol FifthCol SixthCol SeventhCol EigthCol NinthCol TenthCol EleventhCol
2005 0.2 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.5 0.0 0.3
2006 0.3 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.5 0.0 0.3
2007 0.5 0.0 0.0 0.0 0.0 0.2 0.0 0.0 -2.1 0.0 0.8
2008 1.2 0.0 0.0 0.0 0.0 0.6 0.0 0.0 -1.8 0.0 1.9
2009 0.5 0.0 0.0 0.0 0.0 0.3 0.0 0.0 -2.1 0.0 0.9
2010 3.9 0.0 0.0 0.0 0.0 1.0 0.0 0.0 -1.4 0.0 5.6
2011 0.6 0.0 0.0 0.0 0.0 0.3 0.0 0.0 -1.9 -0.1 0.8
2012 11.1 0.0 0.0 0.0 0.0 2.0 0.0 0.0 -1.0 0.0 13.7
2013 0.2 0.0 0.0 0.0 0.0 0.1 0.0 0.0 -2.6 0.0 0.2
}\datatable
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.9*\textwidth,
bar width=0.26cm,
tick label style={/pgf/number format/1000 sep=,font=\footnotesize},
stack negative=on previous,
ybar stacked,
bar width=1em,
ymin=-10,
axis on top,
ymax=30,
ymajorgrids = true,
enlarge x limits={true, abs value=0.75},
height=8cm,
nodes near coords={},
legend style={
at={(1.1,0.5)},
anchor=west,
draw=none },
xtick=data,
xticklabels={GWP,ODP,POCP, AP, EP(T), EP(FW), EP(M), ADP, CED}
]
\addplot [fill=cyan!70,draw=black!70] table [y index=1] {\datatable};
\addplot [fill=green!90,draw=black!70] table [y index=2] {\datatable};
\addplot [fill=teal!90,draw=black!70]table [y index=3] {\datatable};
\addplot [fill=violet!90,draw=black!70]table [y index=4] {\datatable};
\addplot [fill=lime!90,draw=black!70]table [y index=5] {\datatable};
\addplot [fill=magenta!90,draw=black!70]table [y index=6] {\datatable};
\addplot [fill=red!90,draw=black!70]table [y index=7] {\datatable};
\addplot [fill=blue!90,draw=black!70]table [y index=8] {\datatable};
\addplot [fill=yellow!90,draw=black!70] table [y index=9] {\datatable};
\addplot [fill=purple!90,draw=black!70] table [y index=10] {\datatable};
\addplot [fill=olive!90,draw=black!70,show sum on top] table [y index=11] {\datatable};
\legend{Facade,Column,Core,Drywall,Foundation,Roof,Slab,Window,Operation,Disposal,Replacement}
\end{axis}
\end{tikzpicture}
\caption{Impacts by category relative to baseline building with conventional building envelope}
\label{relative}
\end{figure}
\end{document}