Mosaic Plot in Python
By: Dhiraj Patil
Tags: Analytics, Mosaic Plot, Data Visualization, Mosaic Plot In Python, Data Exploration

Mosaic Plot in Python

Data Visualisation Tool in Python:- Mosaic Plot

Image courtesy:-Internet

Hello Everyone,

In this article, we will understand the Mosaic Plot, the difference between crosstab and Mosaic Plot, How to plot the Mosaic Plot in Python, Use of the Mosaic Plot.

Prerequisites:- Python basic coding knowledge

Suppose we have a dataset in which there is one column ‘Gender’ and another feature is ‘Education’ containing ‘Graduate’ and ‘Not_Graduate’ as a factor.

Male Graduate:-2 (50%), Female Graduate:-3 (75%)

We need to find some insight between these two feature i.e we have to find which class of ‘Gender’ have more percentage of Graduates and which class have less percentage of ‘Graduates’. So how do we do that?

One way is the crosstab function of panda’s library. But it just gives numbers. How to make it graphical and effective visualization?

Here Mosaic Plot can help us.

Table of Contents:-

What are the Mosaic Plot and use? Difference between crosstab and mosaic plot How to plot the Mosaic Plot in Python with one example

Well,

What is Mosaic Plot:-

Mosaic Plot is a graphical method for visualizing data from two or more Qualitative data. It’s also called crosstabs or two-way table and it’s used to summarize the relationship among several categorical variables. The Mosaic Plot is based on conditional probabilities.

2. Difference between crosstab and Mosaic Plot:-

Mosaic Plot (also known as Marimekko diagram)is nothing but a further version of (pd.crosstab()) function in python. Crosstab function just gives us a table of numbers whereas Mosaic Plot gives it’d graphical diagram which we can use in the data analysis report.

3. How to plot Mosaic Plot in Python:-

To plot the Mosaic Plot, we are going to take a dataset of Credit Risk. You can download it from here

First, we will import all the necessary libraries.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from statsmodels.graphics.mosaicplot import mosaic

from itertools import product

Then we will load the dataset which we have discussed above.

Let’s have a look at count wise relation between ‘Gender’ and ‘Education’ using the crosstab function.

crosstable=pd.crosstab(df['Gender'],df['Education'])

crosstable

crosstable

Here we can observe the number of Male Graduates is 376 out of a total of Male 489 and the number of Female Graduates is 92 out of a total Female 112. So Male Graduate percentage is 76% and the Female Graduate percentage is 82% which is more than the Male Graduate percentage.

Now we will visualize this data with the help of the Mosaic Plot.

We have already imported mosaic from statsmodels library. mosaic is a function used to plot the Mosaic Plot.

It contains four parameters

(data,featuresToBeCompare,labelizer, properties) as given below

properties parameter is used to fix color

labelizer parameter is used to give label and count number in a plot

props={}

props[('Male','Yes')]={'facecolor':'red', 'edgecolor':'white'}

props[('Male','No')]={'facecolor':'red', 'edgecolor':'white'} props[('Female','Yes')]={'facecolor':'xkcd:aqua','edgecolor':'white'} props[('Female','No')]= {'facecolor':'xkcd:aqua','edgecolor':'white'} labelizer=lambda k:{('Male','Yes'):357,('Female','Yes'):31,('Male','No'):130,('Female','No'):80}[k] mosaic(df,['Gender','married'],labelizer=labelizer,properties=props)

After running this code we will get the following Mosaic Plot

We can replace count numbers by percentage also and add in our analysis report, which will very nice visualization.

Thank you!

Connect to me on LinkedIn if you have any queries or want some more insights

Clap if you liked the article!