Matplotlib Pie Chart

Case Study (Tele Company) : Churn Analysis

- Churn analysis is the evaluation of a company's customer loss rate 
In [1]:
import pandas as pd

url= "https://datascienceschools.github.io/tele_customers.csv"

df = pd.read_csv(url)

df.head()
Out[1]:
customerID gender SeniorCitizen Partner Dependents tenure PhoneService MultipleLines InternetService OnlineSecurity ... DeviceProtection TechSupport StreamingTV StreamingMovies Contract PaperlessBilling PaymentMethod MonthlyCharges TotalCharges Churn
0 7590-VHVEG Female 0 Yes No 1 No No phone service DSL No ... No No No No Month-to-month Yes Electronic check 29.85 29.85 No
1 5575-GNVDE Male 0 No No 34 Yes No DSL Yes ... Yes No No No One year No Mailed check 56.95 1889.5 No
2 3668-QPYBK Male 0 No No 2 Yes No DSL Yes ... No No No No Month-to-month Yes Mailed check 53.85 108.15 Yes
3 7795-CFOCW Male 0 No No 45 No No phone service DSL Yes ... Yes Yes No No One year No Bank transfer (automatic) 42.30 1840.75 No
4 9237-HQITU Female 0 No No 2 Yes No Fiber optic No ... No No No No Month-to-month Yes Electronic check 70.70 151.65 Yes

5 rows × 21 columns

Number of customers

- who continued their subscriptions -> Churn : No
- who discontinued their subscriptions -> Churn : Yes
In [49]:
df['Churn'].value_counts()   
Out[49]:
No     5174
Yes    1869
Name: Churn, dtype: int64

Data Visualizations

- import matplotlib.pyplot as plt -> for plotting the graph 

- import seaborn as sns -> for plotting interactive graph

- from pylab import rcParams -> for customizing Matplotlib plots 

- Data to plot

   labels = df['Churn'].value_counts(sort = True).index -> Labeling each slice

   sizes = df['Churn'].value_counts(sort = True) -> Size of each slice

   colors = ["lightblue","pink"] -> Color of each slice

   explode = (0.05,0) -> This will explode first slice

- rcParams['figure.figsize'] = 6,6  -> Changing size of figure
- plt.axis('equal') -> Making a square plot with equal axes


- Plot 

plt.pie(sizes, explode=explode, labels=labels, colors=colors, 
               autopct='%1.1f%%', shadow=True, startangle=90,)
plt.title('Customer Churn Breakdown')
plt.show()

- Legend

patches, texts = plt.pie(sizes, explode=explode, colors=colors,shadow=True, startangle=90,)
plt.legend(patches, labels, loc="best")

Simple Pie Chart

In [55]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
 
plt.pie(sizes, labels=labels, colors=colors)
plt.title('Customer Churn Breakdown')
plt.show()

Adding rcParams

- Changing size of figure
In [56]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams

labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
 
rcParams['figure.figsize'] = 8,8

plt.pie(sizes, labels=labels, colors=colors)
plt.title('Customer Churn Breakdown')
plt.show()

Adding plt.axis('equal')

- Making a square plot with equal axes
In [53]:
import matplotlib.pyplot as plt 
import seaborn as sns 

labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
 
plt.pie(sizes, labels=labels, colors=colors)
plt.title('Customer Churn Breakdown')
plt.axis('equal')
plt.show()

Add Percentage

- plt.pie -> Add autopct='%1.1f%%'
In [11]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
 
rcParams['figure.figsize'] = 6,6

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title('Customer Churn Breakdown')
plt.show()

Explode First Slice

- explode = (0.1,0) 
- plt.pie -> Add explode = explode
In [12]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
explode = (0.1,0) 
 
rcParams['figure.figsize'] = 6,6

plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title('Customer Churn Breakdown')
plt.show()

Add Shadow

- plt.pie -> Add shadow=True
In [13]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
explode = (0.1,0) 
 
rcParams['figure.figsize'] = 6,6

plt.pie(sizes, explode=explode, labels=labels, colors=colors,
               autopct='%1.1f%%',shadow=True)
plt.title('Customer Churn Breakdown')
plt.show()

Specify Angel

- plt.pie -> Add startangle = 90
In [15]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
explode = (0.1,0) 
 
rcParams['figure.figsize'] = 6,6

plt.pie(sizes, explode=explode, labels=labels, colors=colors,  
               autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('Customer Churn Breakdown')
plt.show()

Adding Legend

- patches, texts = plt.pie(sizes, explode=explode, colors=colors,shadow=True, startangle=90,)

- plt.legend(patches, labels, loc="best")
In [61]:
import matplotlib.pyplot as plt 
import seaborn as sns 
from pylab import rcParams


#labels = df['Churn'].value_counts(sort = True).index
sizes = df['Churn'].value_counts(sort = True)
colors = ["lightblue","pink"]
explode = (0.1,0) 
 
rcParams['figure.figsize'] = 6,6

patches, texts = plt.pie(sizes, explode=explode, colors=colors, shadow=True, startangle=90)
plt.legend(patches, labels, loc="best")

plt.title('Customer Churn Breakdown')
plt.show()