latextikz

TikZ picture pie chart - Exclude labels for categories with 0


I need to display a pie chart with 5 categories but where some have a result of 0. But there is this annoying "0" showing up on the pie chart. How can I get rid of it? I suspect it's the 0 from the "Neither agree nor disagree" category. I either want to make it so 0 categories don't show up at all, or so I can hide just that one, but I haven't been able to find a resource to do it online. Thank you!

Image: Pie chart with annoying 0

Code:

\documentclass[conference]{IEEEtran}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgf-pie}

\definecolor{ELMpos}{RGB}{178, 255, 102} % Light lime green
\definecolor{ROUNDneg}{RGB}{150, 54, 86} % Burgundy
\definecolor{ELMneg}{RGB}{229, 102, 102} % Soft red
\definecolor{ANON}{RGB}{102, 204, 178} % Mint green
\definecolor{CONCISE}{RGB}{224, 255, 255} % Pale cyan

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \pie[
            text=legend,
            radius=2,
            color={ROUNDneg, ELMneg, ANON, ELMpos, CONCISE},
            sum=auto
        ]{
            0/Strongly disagree,
            2/Disagree,
            0/Neither agree nor disagree,
            7/Agree,
            10/Strongly agree
        }
    \end{tikzpicture}
    \caption{}
\end{figure}


\end{document}

Solution

  • You could redefine \pgfpie@slice to check if the number is > 0:

    \documentclass[conference]{IEEEtran}
    
    \usepackage{xcolor}
    \usepackage{tikz}
    \usepackage{pgf-pie}
    
    \definecolor{ELMpos}{RGB}{178, 255, 102} % Light lime green
    \definecolor{ROUNDneg}{RGB}{150, 54, 86} % Burgundy
    \definecolor{ELMneg}{RGB}{229, 102, 102} % Soft red
    \definecolor{ANON}{RGB}{102, 204, 178} % Mint green
    \definecolor{CONCISE}{RGB}{224, 255, 255} % Pale cyan
    
    
    \makeatletter
    % args:
    % #1: begin angle
    % #2: end angle
    % #3: number
    % #4: label
    % #5: explode
    % #6: fill color
    % #7: radius
    % #8: center
    \def\pgfpie@slice#1#2#3#4#5#6#7#8{%
      \pgfmathparse{0.5*(#1)+0.5*(#2)}
      \let\pgfpie@midangle\pgfmathresult
    
      \path (#8) -- ++({\pgfpie@midangle}:{#5}) coordinate (pgfpie@O);
    
      \pgfmathparse{(#7)+(#5)}
      \let\pgfpie@radius\pgfmathresult
      
      % slice
      \draw[line join=round,fill={#6},\pgfpie@style] (pgfpie@O) -- ++({#1}:{#7}) arc ({#1}:{#2}:{#7}) -- cycle;
    
      \pgfpie@ifchangedirection{%
        \pgfmathparse{min(((#1)-(#2)-10)/110*(-0.3),0)}
      }{%
        \pgfmathparse{min(((#2)-(#1)-10)/110*(-0.3),0)}
      }%
      
      \pgfmathparse{(max(\pgfmathresult,-0.5) + 0.8)*(#7)}
      \let\pgfpie@innerpos\pgfmathresult
    
      \pgfpie@ifx\pgfpie@text\pgfpie@text@inside{%
        % label and number together
        \path (pgfpie@O) -- ++({\pgfpie@midangle}:{\pgfpie@innerpos}) node[align=center]
        {\pgfpie@scalefont{#3}\pgfpie@labeltext{#4}\\\pgfpie@numbertext{#3}};
      }{%
        % label
        \pgfpie@ifhidelabel{}{%
          \pgfpie@iflegend{}{%
            \path (pgfpie@O) -- ++ ({\pgfpie@midangle}:{\pgfpie@radius})
            node[inner sep=0, \pgfpie@text={\pgfpie@midangle:#4}]{};
          }%
        }%
    
        % number
        \ifnum#3>0
          \path (pgfpie@O) -- ++({\pgfpie@midangle}:{\pgfpie@innerpos}) node
          {\pgfpie@scalefont{#3}\pgfpie@numbertext{#3}};
        \fi
      }%
    }
    \makeatother
    
    \begin{document}
    
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \pie[
                text=legend,
                radius=2,
                color={ROUNDneg, ELMneg, ANON, ELMpos, CONCISE},
                sum=auto
            ]{
                0/Strongly disagree,
                2/Disagree,
                0/Neither agree nor disagree,
                7/Agree,
                10/Strongly agree
            }
        \end{tikzpicture}
        \caption{}
    \end{figure}
    
    
    \end{document}
    

    enter image description here