[Tex/LaTex] Tikz Pie Charts

pgf-pietikz-pgf

Is there a way to make a pie chart with Tikz so that the labels are not numbers (e.g. 30/70 in my example) but the labels are displayed as variables (e.g. omega and 1- omega)?

Here is what I currently have:

\begin{tikzpicture}
    \pie{30/Stock, 70/Bond}
\end{tikzpicture}

enter image description here

I do not want it to say 30/70, and want to be able to label it with a Greek letter instead.

Best Answer

To answer your original question, I don't know if there is any built-in method in pgf-pie to have only textual labels and not the numbers, but it is possible to patch the macro responsible for making a slice to achieve that:

output of first code with pgf-pie

\documentclass[border=5mm]{standalone}
\usepackage{pgf-pie}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\pgfpie@slice}% this is the macro we're patching
{\scalefont{#3}\shortstack{#4\\\beforenumber#3\afternumber}}% find this
{\scalefont{#3}#4}% and replace with this
{}{}
\makeatother
\begin{document}
\begin{tikzpicture}
    \pie[text=inside]{30/$\omega$, 70/$1-\omega$}
\end{tikzpicture}
\end{document}

If you want to use the code from the example Alan mentioned, you'll need to modify it to use a third loop variable, for defining the colour. I indicated in the code where I made modifications.

output of code

% Original code from http://www.texample.net/tikz/examples/pie-chart/
% by Robert Vollmert
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\slice}[5]{% changed 4 to 5, fifth argument to define colour
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice
  % add #5 after fill=
  \draw[thick,fill=#5] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

  % outer label
  \node[label=\midangle:#4] at (\midangle:1) {};

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{max(\temp,-0.5) + 0.8}
  \let\innerpos\pgfmathresult
  \node at (\midangle:\innerpos) {#3};
}

\begin{tikzpicture}[scale=3]

\newcounter{a}
\newcounter{b}
% add third loop variable for colour
\foreach \p/\t/\clr in {30/$\omega$/blue!20, 70/$1-\omega$/red!30}
  {
    \setcounter{a}{\value{b}}
    \addtocounter{b}{\p}
    \slice{\thea/100*360}
          {\theb/100*360}
          {\t}{}{\clr} % added fifth argument, {\clr}
  }

\end{tikzpicture}
\end{document}

Finally, for a simple two-part pie here is one way of doing it from scratch.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[draw=black,fill=blue!30] (0,0) -- (0:2cm) arc[start angle=0,end angle=120,radius=2cm] -- cycle;
\filldraw[draw=black,fill=red!30] (0,0) -- (120:2cm) arc[start angle=120,end angle=360,radius=2cm] -- cycle;
\node at (60:1cm) {$\omega$};
\node at (240:1cm) {$1-\omega$};
\end{tikzpicture}
\end{document}
Related Question