# Stacked Bar Chart - How to center values inside/outside the bar?

I modified a code (found here in the forum) to produce a stacked bar chart. Unfortunately I do not understand the whole code…
How can I center the values within the bar chart or right beside the chart? Unfortunately there are values outside the chart (0.430, 0.054, …)!

Furthermore: How can I change the Formate? 0.430 --> 43.00% and so on?

\documentclass[a4paper]{scrbook}
\usepackage{pgfplotstable}
\usepackage[
backend=biber
]
{biblatex}

\pgfplotsset{compat=1.8}

\centering
\scriptsize

Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}\data

\pgfplotstablecreatecol[create col/expr={\thisrow{1} + \thisrow{2} + \thisrow{3}}]{sum}{\data}

\pgfplotsset{
percentage plot/.style={point meta=explicit,every node near coord/.append style={font=\scriptsize,},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber[fixed,fixed zerofill,precision=3]{\pgfplotspointmeta}
\fi},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=100.01,
visualization depends on={y \as \originalvalue},
},
percentage series/.style={
table/x expr=\coordindex,
table/y expr=(\thisrow{#1}/\thisrow{sum}*100),
table/meta=#1
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=0.80\textwidth,
height=0.33\textheight,
percentage plot,
bar width=7ex,
xticklabels from table={\data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=\scriptsize},
legend cell align={left},
legend pos=outer north east
]

\legend{y, p,n}

\end{axis}

\end{tikzpicture}
\end{document}


• Could you please explicitly link to the source of your code? I am also asking for that in order to make sense of \thisrow{sum}, in your table there is no explicit sum row. This would most likely allow me to address the second part of your question (which I missed in my answer, sorry) in a not too complicated fashion. – marmot Dec 9 '18 at 16:06

You only need to change the anchor of the nodes near coords. (I also changed the text color of the lowest nodes for a better readability.)

\documentclass[a4paper]{scrbook}
\usepackage{pgfplotstable}
\usepackage[
backend=biber
]
{biblatex}

\pgfplotsset{compat=1.8}

\centering
\scriptsize

Label,1,2,3
a,0.439,0.130,0.430
b,0.874,0.072,0.054
c,0.757,0.061,0.183
d,0.783,0.139,0.078
e,0.451,0.115,0.434
f,0.398,0.115,0.487
}\data

\pgfplotstablecreatecol[create col/expr={\thisrow{1} + \thisrow{2} + \thisrow{3}}]{sum}{\data}

\pgfplotsset{
percentage plot/.style={point meta=explicit,
every node near coord/.append style={font=\scriptsize,anchor=north,inner
ysep=1pt},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber[fixed,fixed zerofill,precision=3]{\pgfplotspointmeta}
\fi},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=100.01,
visualization depends on={y \as \originalvalue},
},
percentage series/.style={
table/x expr=\coordindex,
table/y expr=(\thisrow{#1}/\thisrow{sum}*100),
table/meta=#1
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=0.80\textwidth,
height=0.33\textheight,
percentage plot,
bar width=7ex,
xticklabels from table={\data}{Label},
xtick=data,
x tick label style={align=center},
legend style={draw=none, font=\scriptsize},
legend cell align={left},
legend pos=outer north east
]