1

I know it is possible to have only the sum shown on top of the bar chart and remove the other values in the stacked bar chart (as shown in picture attached), but I can't figure out what went wrong in my code. enter image description hereHere's my 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

The following lines in second axis is making the values to appear for all the coords.

nodes near coords={
          \pgfmathprintnumber[precision=0]{\pgfplotspointmeta}},

Replace it with

nodes near coords={}, 

to suppress them. Actually you don't need two axis to plot negative and positive separately. It can be done with stack negative=on previous, option.

\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}

enter image description here

  • Thanks for your help. It's exactly what i wanted. However the negative values (below y=0) are being ignored now, do you know how can i have them be taken into account? – thesilencer Dec 11 '18 at 14:48
  • @thesilencer I didn’t understand. you want negative values to be shown? Or are you saying that the sum doesn’t include negative numbers? – nidhin Dec 11 '18 at 15:09
  • I meant the sum doesn't include negative numbers in the ninth column in the table. The sum should be smaller in reality. – thesilencer Dec 11 '18 at 17:00
  • @thesilencer that is because of the way sum on top is defined. It will just put the final y value there. – nidhin Dec 11 '18 at 17:02
  • Hmm. But the final y value should also include the negative numbers... Is there anyway to include them? – thesilencer Dec 11 '18 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.