Forked from rishabhrao1997/plot_categorical_variables_pie.py
Created
November 16, 2022 15:17
-
-
Save maria-aguilera/71750a3fc26adb4bd1d1b98366bdab4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_categorical_variables_pie(data, column_name, plot_defaulter = True, hole = 0): | |
''' | |
Function to plot categorical variables Pie Plots | |
Inputs: | |
data: DataFrame | |
The DataFrame from which to plot | |
column_name: str | |
Column's name whose distribution is to be plotted | |
plot_defaulter: bool | |
Whether to plot the Pie Plot for Defaulters or not | |
hole: int, default = 0 | |
Radius of hole to be cut out from Pie Chart | |
''' | |
if plot_defaulter: | |
cols = 2 | |
specs = [[{'type' : 'domain'}, {'type' : 'domain'}]] | |
titles = [f'Distribution of {column_name} for all Targets', f'Percentage of Defaulters for each category of {column_name}'] | |
else: | |
cols = 1 | |
specs = [[{'type': 'domain'}]] | |
titles = [f'Distribution of {column_name} for all Targets'] | |
values_categorical = data[column_name].value_counts() | |
labels_categorical = values_categorical.index | |
fig = make_subplots(rows = 1, cols = cols, | |
specs = specs, | |
subplot_titles = titles) | |
#plotting overall distribution of category | |
fig.add_trace(go.Pie(values = values_categorical, labels = labels_categorical, hole = hole, | |
textinfo = 'label+percent', textposition = 'inside'), row = 1, col = 1) | |
#plotting distribution of category for Defaulters | |
if plot_defaulter: | |
percentage_defaulter_per_category = data[column_name][data.TARGET == 1].value_counts() * 100 / data[column_name].value_counts() | |
percentage_defaulter_per_category.dropna(inplace = True) | |
percentage_defaulter_per_category = percentage_defaulter_per_category.round(2) | |
fig.add_trace(go.Pie(values = percentage_defaulter_per_category, labels = percentage_defaulter_per_category.index, | |
hole = hole, textinfo = 'label+value', hoverinfo = 'label+value'), row = 1, col = 2) | |
fig.update_layout(title = f'Distribution of {column_name}') | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment