How to visualize the relationship between two categorical variables in Python

This situation occurs while performing classification. Here the target variable is categorical, hence the predictors can either be continuous or categorical. Hence, when the predictor is also categorical, then you use grouped bar charts to visualize the correlation between the variables.

Consider the below example, where the target variable is “APPROVE_LOAN”. One of the predictors is “GENDER”, so in order to understand whether there is an effect of Gender on the approval of a loan or not, you plot grouped bar chart.

No-Correlation example

Sample Output

Visualizing relationship between two categorical variables using a grouped bar chart
Visualizing relationship between two categorical variables using a grouped bar chart

If the bars of the category “M” is similar to the bars of the category “F”, then you can say the GENDER and APPROVE_LOAN are NOT correlated.

The reason behind it is simple. If the bars are similar, that means if we change the gender, we cannot say that the loans are more approved or less approved, the ratio of approval Vs non-approval is the same for both the genders.

If the grouped bars are of different length for each category, then the variables are correlated to each other

Correlated variables example

Consider another scenario of the same data shown below, here the ratios of approval vs non-approval of loans are different for category “M” and “F”. Hence, you can say that changing the gender will impact the loan approval. Hence, there is a correlation between these two variables.

Sample Output

Grouped bar charts showing the correlation between GENDER and APPROVE_LOAN
Grouped bar charts showing the correlation between GENDER and APPROVE_LOAN

Now, here you can see the difference in the ratios! Simply put, your loan will get approved if you are Female! And if you are a Male then there are 50/50 chances of approval. Gender affects the approval rate. Hence, gender and loan approval are correlated here.

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

1 thought on “How to visualize the relationship between two categorical variables in Python”

Leave a Reply!

Your email address will not be published. Required fields are marked *