2

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.

  • 1
    Welcome to TeX.SE!. Please try to do something by yourself and then add a minimal working example (MWE) to your post. Without showing any of your effort to draw this diagram, your question is "do-it-for-me" which is unlikely to be answered. – Cragfelt Dec 15 '17 at 21:10
  • I did put in my code. Do you see it? – Zak Fischer Dec 15 '17 at 21:14
  • You can easily modify the code here. – Alan Dec 15 '17 at 21:22
  • Alan could you give a bit more explanation - that diagram has percentages showing up too. How would that be adjusted to get what I'm looking for? – Zak Fischer Dec 15 '17 at 21:25
  • 1
    He might not have seen your comment. You as the owner of the post is notified of all comments, other commenters are not notified unless you write an at-sign followed by the username, e.g. @Alan. That said, try replacing {\p\%}{\t} with {\t}{}. – Torbjørn T. Dec 15 '17 at 21:48
3

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}
  • Perfect! This got the job done, and then I added in two nodes for the stock/bond labes: \node at (240:2.5cm) {Bond}; \node at (60:2.5cm) {Stock}; – Zak Fischer Dec 18 '17 at 1:30
  • @ZakFischer If the answer worked for you, please consider accepting it by clicking on the check mark left of it. – marmot Jan 9 at 0:03
  • @marmot Got it - done! – Zak Fischer Jan 9 at 0:06

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.