Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def histogram_plots(columns_to_plot, super_title):
'''
Prints a histogram for each independent variable of the list columns_to_plot.
Parameters:
columns_to_plot (list of string): Names of the variables to plot
super_title (string): Super title of the visualization
Returns:
None
'''
# set number of rows and number of columns
number_of_columns = 2
number_of_rows = math.ceil(len(columns_to_plot)/2)
# create a figure
fig = plt.figure(figsize=(12, 5 * number_of_rows))
fig.suptitle(super_title, fontsize=22, y=.95)
# loop to each demographic column name to create a subplot
for index, column in enumerate(columns_to_plot, 1):
# create the subplot
ax = fig.add_subplot(number_of_rows, number_of_columns, index)
# histograms for each class (normalized histogram)
df_telco[df_telco['Churn']=='No'][column].plot(kind='hist', ax=ax, density=True,
alpha=0.5, color='springgreen', label='No')
df_telco[df_telco['Churn']=='Yes'][column].plot(kind='hist', ax=ax, density=True,
alpha=0.5, color='salmon', label='Yes')
# set the legend in the upper right corner
ax.legend(loc="upper right", bbox_to_anchor=(0.5, 0.5, 0.5, 0.5),
title='Churn', fancybox=True)
# set title and labels
ax.set_title('Distribution of ' + column + ' by churn',
fontsize=16, loc='left')
ax.tick_params(rotation='auto')
# eliminate the frame from the plot
spine_names = ('top', 'right', 'bottom', 'left')
for spine_name in spine_names:
ax.spines[spine_name].set_visible(False)
# customer account column names
account_columns_numeric = ['tenure', 'MonthlyCharges', 'TotalCharges']
# histogram of costumer account columns
histogram_plots(account_columns_numeric, 'Customer Account Information')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment