# Multiple values inside stacked bar plot -Tikz

I want to have a stacked bar plot as the attached. However, can not figure out what make the bars to display multiple values?

The following is the code.

\begin{tikzpicture}

\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{axis}[
ybar stacked, ymin=0,
bar width=8mm,
symbolic x coords={2016,2017,2018,2019,2020},
xtick=data,
nodes near coords,
legend style={at={(0.05,0.8)},anchor=west}
]

\addplot [fill=blue!20, show sum on top] coordinates {
({2016},1)
({2017},0)
({2018},1)
({2019},1)
({2020},1)};
\addplot [fill=red!20,show sum on top] coordinates {
({2016},0)
({2017},3)
({2018},3)
({2019},4)
({2020},1)};
\addplot [fill=green!20,show sum on top] coordinates {
({2016},0)
({2017},1)
({2018},4)
({2019},4)
({2020},1)};
\addplot [fill=orange!20,show sum on top] coordinates {
({2016},0)
({2017},0)
({2018},0)
({2019},0)
({2020},2)};
\legend{CCTV \& Visual,Remote sensing, Social media, WSN \& IoT}
\end{axis}
\end{tikzpicture}


The plot generated by the code. I want to get rid of multiple values inside the bars and have only the correct value.

You seem to have copied the style show sum on top from this answer or some related post. This answer explicitly only uses the the style in the second (=last) \addplot command. Here this approach does not work since the last \addplot has zeros. So one needs take care of that. There are two options:

1. if the data is presented in table form, pgfplots could be made figure out when to place the sums. Given the tables, there are excellent posts that compute the sums for you. So let's focus on the case that there is no table, as in the actual question. Then
2. you can selectively add the sums at the appropriate plots with a style that gets defined in this answer, selective show sum on top.

Since the data is not in table form, here is option 2. The selective show sum on top style accepts an argument (or can be made thread over a list with the /.list key, as shown below) which says at which positions among the nonzero entries the sum should be computed and displayed. If you do not specify the argument, 0 will be used.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}

\pgfplotsset{
selective show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\ifnum\coordindex=#1
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
\fi
},
},selective show sum on top/.default=0
}

\begin{axis}[width=10cm,
ybar stacked, ymin=0,
bar width=8mm,
symbolic x coords={2016,2017,2018,2019,2020},
xtick=data,
nodes near coords,
legend style={at={(0.05,0.8)},anchor=west}
]

\addplot [fill=blue!20,selective show sum on top] coordinates {
({2016},1)
({2017},0)
({2018},1)
({2019},1)
({2020},1)};
({2016},0)
({2017},3)
({2018},3)
({2019},4)
({2020},1)};
\addplot [fill=green!20,selective show sum on top/.list={0,1,2}] coordinates {
({2016},0)
({2017},1)
({2018},4)
({2019},4)
({2020},1)};
\addplot [fill=orange!20,selective show sum on top] coordinates {
({2016},0)
({2017},0)
({2018},0)
({2019},0)
({2020},2)};
\legend{CCTV \& Visual,Remote sensing, Social media, WSN \& IoT}
\end{axis}
\end{tikzpicture}
\end{document}