# 7. Pie Chart

[1]:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

plt.style.use('ggplot')
np.random.seed(37)


## 7.1. Basic

[2]:

s = pd.Series([10, 20, 30], index=['Dog', 'Cat', 'Horse'])

fig, ax = plt.subplots(figsize=(5, 5))

_ = s.plot(kind='pie', ax=ax)
_ = ax.yaxis.set_visible(False)


## 7.2. Show percentages

[3]:

s = pd.Series([10, 20, 30], index=['Dog', 'Cat', 'Horse'])

fig, ax = plt.subplots(figsize=(5, 5))

_ = s.plot(kind='pie', autopct='%1.1f%%', ax=ax)
_ = ax.yaxis.set_visible(False)


## 7.3. Change labels

[4]:

s = pd.Series([10, 20, 30], index=['Dog', 'Cat', 'Horse'])

fig, ax = plt.subplots(figsize=(5, 5))

labels = [f'{i} ({v})' for i, v in zip(s.index, s.values)]

_ = s.plot(kind='pie', autopct='%1.1f%%', labels=labels, ax=ax)
_ = ax.yaxis.set_visible(False)


## 7.4. Treemap

Let’s use squarify to plot a treemap.

[5]:

import squarify

s = pd.Series([5, 47, 41, 11, 33, 35, 62], index=['Dog', 'Cat', 'Horse', 'Cow', 'Chicken', 'Iguana', 'Bird'])
colors = sns.color_palette('hls', len(s))
labels = [f'{i}\n{v}' for i, v in zip(s.index, s.values)]

fig, ax = plt.subplots(figsize=(5, 5), dpi=100)

_ = squarify.plot(sizes=s.values, label=labels, color=colors, ax=ax)

_ = plt.title('Treemap')
_ = plt.axis('off')