-
-
Save amandaiglesiasmoreno/1d6974ca5dadcc61188d34e7c62694fe to your computer and use it in GitHub Desktop.
This file contains 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 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