A Beginner’s Guide to Data Visualization with Python | by Natassha Selvaraj | Towards Data Science

There is magic in graphs. The profile of a curve reveals in a flash a whole situation — the life history of an epidemic, a panic, or an era of prosperity. The curve informs the mind, awakens the imagination, convinces.
Data visualization is the act of taking data and presenting it in a visual format. It is a powerful technique used to communicate information, and allows a reader to immediately gain insight from data.
Let me give you a small example to demonstrate just how powerful data visualization is.
Here is some sample data, with three variables — ‘Age Group’, ‘Use Internet’, and ‘Do Not Use.’
Disclaimer: This is not real data, I just created it to demonstrate the importance of visualization.
Can you find the relationship between age group and internet usage based on the data above?
You can probably see some trends in the data. You might also be able to find the age group with the most frequent Internet usage.
However, it is hard to get a clear picture by simply looking at the data above.
Now, here are two graphs I created to model this relationship, using just one line of code.
See the difference?
Generating a simple graph from messy data can give us valuable insights, and help us find trends we never knew existed.
Data visualization is generally used during two phases of the data science process:
When you, the data scientist, are trying to understand the data.
When you are trying to present your data findings to another person.
In this tutorial, I will show you how to perform exploratory data visualization in Python, using built-in libraries such as Matplotlib and Seaborn.
I will be using the train.csv file from Kaggle’s Titanic dataset.
import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline
Note: Make sure you have all these libraries installed. If not, you can install them using a simple pip command.
df = pd.read_csv("train.csv")df.head()
Taking a look at the head of the data frame, this is the information we get.
There are 12 variables in the data frame. These are:
PassengerId: Each passenger’s unique IDPclass: The passenger class; 1st, 2nd and 3rd. The first was the upper class, and the third was the lower class.NameSexAgeSibSp: Number of siblings and/or spouses on boardParch: Number of parents and/or children on boardTicket: Ticket NumberFareCabinEmbarked: Port of embarkationSurvived: Whether or not a passenger survived the Titanic. A value of 1 means the passenger survived, and a value of 0 means they did not.
PassengerId: Each passenger’s unique ID
Pclass: The passenger class; 1st, 2nd and 3rd. The first was the upper class, and the third was the lower class.
Name
Sex
Age
SibSp: Number of siblings and/or spouses on board
Parch: Number of parents and/or children on board
Ticket: Ticket Number
Fare
Cabin
Embarked: Port of embarkation
Survived: Whether or not a passenger survived the Titanic. A value of 1 means the passenger survived, and a value of 0 means they did not.
If you want to know more about your data set, (the number of rows, for example), I suggest using Pandas profiling.
It is a powerful tool that provides an analysis of your data frame. You won’t have to run lines of code, again and again, to check for missing values or variable type.
import pandas_profiling as pppp.ProfileReport(df)
With just these two lines of code, a report will be generated on your data frame, which looks like this:
The data frame overview provides you with a quick analysis of the entire data set, along with missing values, number of variables, and variable types.
The next section of the report will provide you with an in depth analysis of each variable in the data set, and it looks like this:
The report also includes visualizations, to map the correlation between the different variables in the dataset. Here is an example of a heatmap generated:
Even before we start analyzing each variable in the data frame, the heatmap tells us a lot about the relationship between the different variables.
By just looking at it, we can tell that some values are more highly correlated than others. This can be very helpful when making predictions or answering certain data questions, and will be explained in more detail as we go along.
Before starting any kind of data analysis or visualization, you must have a data question in mind.
What kind of insights are you trying to draw from the data? Any specific trends or patterns?
Write down a few questions you would like to answer with the help of a visualization.
In this case, these are the following data questions we want to be able to answer by the end of the tutorial:
How many people survived the Titanic?Which gender was more likely to survive the Titanic?Did the class the passenger was in have any impact on their survival?Were younger people more likely to survive the Titanic?Were passengers who paid higher fares more likely to survive the Titanic?
How many people survived the Titanic?
Which gender was more likely to survive the Titanic?
Did the class the passenger was in have any impact on their survival?
Were younger people more likely to survive the Titanic?
Were passengers who paid higher fares more likely to survive the Titanic?
Now that we have an idea of what to look for, we can start the data visualization.
The two main types of variables you will encounter when working with data are categorical and quantitative variables.
Categorical: Categorical variables, as the name suggests, is one that has two or more categories. It represents types of data that can be divided into groups.
For example: In the Titanic data set, the variable ‘Sex’ is categorical. It represents two categories of people; male and female.
Quantitative: Quantitative, or numerical variables represent a measurable quantity.
For example: The variable ‘Age’ is a quantitative variable.
There are different ways to visualize quantitative and qualitative variables, which will be discussed as we go along.
We will start by visualizing a single variable first, by using a ‘count plot’ in Seaborn.
A count plot is a simple way to visualize categorical variables. As the name suggests, it displays the frequency of a variable.
Visualizing how many people survived the Titanic:
sns.countplot(x='Survived',data=df)
From this count plot, it can be seen that there were a lot more people who didn’t survive the Titanic than ones who did.
Using a count plot, we can now visualize the other categorical variables in the data frame:
Gender:
sns.countplot(x='Sex',data=df)
There appears to have been a lot more male than female passengers on the Titanic.
Pclass
# Pclass:sns.countplot(x='Pclass',data=df)
The count plot shows us that there were a lot more passengers in the third class than any other class on the Titanic.
Embarked
Most passengers in the Titanic embarked from port S.
To visualize the distribution of a quantitative variable, we can use a histogram.
A histogram is an equivalent of a count plot, but is used for numeric data.
First, we will create a histogram to visualize the age distribution of passengers in the Titanic:
sns.distplot(df['Age'])
From the histogram above, it can be observed that most passengers in the Titanic were in the 20–40 age group.
Next, we will take a look at the variable ‘Fare’:
sns.distplot(df['Fare'])
It can be observed that most passengers paid a fare of around 0–50. There are some outliers — a few prices that go up to 500.
It would make sense for passengers of a lower ‘Pclass’, such as class 3, to pay lower fare prices than passengers of a higher class.
We can easily visualize this relationship.
When performing data analysis, we often want to find the relationship between two variables.
For example, when answering the question ‘Which gender was more likely to survive the Titanic?’
We need to visualize two variables here — ‘Sex’ and ‘Survived.’
A simple way to do this is to create a count plot, and add a hue function. The hue parameter allows you to choose a variable to visualize with color encoding.
It is a great way to visualize categorical variables.
Here is an example:
# Visualize survived with sex:sns.countplot(x='Survived',hue='Sex',data=df)
From this plot, we can see clearly that there were a higher number of female survivors on the Titanic than male.
Similarly, we can answer the following question — Did the class the passenger was in have any impact on their survival?
sns.countplot(x='Survived',hue='Pclass',data=df)
sns.countplot(x='Pclass',hue='Survived',data=df)
From the plots above, we can see that passengers in the third class were far less likely to survive than passengers in the first and second class.
We can now visualize the variable ‘Embarked’ with ‘Survived.’
# Survived and embarked:sns.countplot(x='Survived',hue='Embarked',data=df)
Next, we will take a look at how to visualize a quantitative variable with a categorical variable.
‘Were younger people more likely to survive the Titanic?’
To answer this question, we need to visualize the variables ‘Survived’ and ‘Age.’ We can use a simple box plot to do this.
A box plot gives us a good indication on the spread of values, and provides information on the 5 number summary (minimum, first quartile, median, third quartile, maximum).
To fully understand box plots, you can read this article.
# Age and sex with survivedsns.boxplot(data=df, x='Survived', y='Age')
From the plot above, there seems to be no clear correlation between survival and the age of a person on the Titanic.
Were passengers who paid higher fares more likely to survive the Titanic?
To answer this question, we need to visualize the variables ‘Survived’ and ‘Fare.’
We can create a bar plot to do so:
sns.barplot(x='Survived',y="Fare",data=df)
It can be observed that passengers who paid higher fares were much more likely to survive than passengers who did not.
This may be due to the passenger class they were in. Passengers in higher classes would be more likely to pay higher fares than the passengers in a lower class.
We can visualize this relationship:
sns.barplot(x='Pclass',y='Fare',data=df)
It looks like our assumption was right!
Passengers in higher classes did pay more for tickets than passengers in lower classes.
Sometimes, we want to visualize more than two variables with each other. For instance, we want to see if a specific age group of males or females were more likely to survive the Titanic.
We need to visualize three variables — ‘Age’, ‘Sex’, and ‘Survived.’ Two of these variables are categorical, and one is quantitative.
You can do this by creating a box plot of the variables ‘Age’ and ‘Sex,’ and create a hue ‘Survived.’
# Age and sex with survived:sns.boxplot(data=df, x='Age', y='Sex', hue ='Survived')
We can do the same thing with ‘Age’, ‘Pclass’, and ‘Survived’ to see the age distribution of the passengers who survived the Titanic.
sns.boxplot(data=df, x='Age', y='Sex', hue ='Pclass')
Most first and second class passengers are older, which makes sense because they would have more money to spend to travel in higher classes.
Finally, to see the relationship between the passenger class, fare paid, and survival, we can run the following code:
sns.barplot(data=df, x='Pclass', y='Fare', hue ='Survived')
From the visualization above, we can observe two things:
Passengers of a higher class pay a much higher fare than passengers who don’t.
Passengers of a higher class are more likely to survive the Titanic.
Visualizing the relationship between more than two variables at the same time is very powerful because it helps us find patterns and answer multiple data questions at the same time.
It is also very simple to create such a visualization using the tools provided in Seaborn.
Pair plots are a type of visualization that plot the pairwise distribution of multiple variables in one plot.
Pair plots in Seaborn only take in numeric columns, so you will need to select those to pass into your dataset.
In this pair plot, we will visualize every numeric variable alongside each other, and choose to color it with the variable ‘Survived’:
numeric = df.iloc[: , [0,1,5,9]]sns.pairplot(numeric.dropna(), hue='Survived')
The resulting pair plot looks like this:
The pair plot provides us with two types of visualizations — a scatter plot, and a distplot. You can read more about the types of plots here.
The distplot on the diagonal gives us information about a single variable, similar to the histograms we created earlier.
The scatter plot shows us the correlation between all numeric variables. There seem to be no strong relationships observed from any of these variables.
We can generate a heatmap in Seaborn, similar to the one provided in the Pandas profiling report.
Before generating the heatmap, we need to create a correlation matrix from our data frame:
cor = df.corr()sns.heatmap(cor)
The heatmap shows us the correlation between the variables in the data set. The variation in colors represents different levels of correlation.
The correlation between variables range from -1 to 1. A value of -1 represents a perfect negative correlation, and a value of 1 represents a perfect positive correlation.
A value of zero indicates that there is no correlation whatsoever between the variables.
You’ll notice that the diagonal in the heatmap seems to have a correlation of 1, and that is because every variable is perfectly correlated with itself.
We can observe some strong relationships here.
The variables ‘SibSp’ and ‘Parch’ seem to have a positive correlation.
The variables ‘Fare’ and ‘Survived’ also seem to have a positive correlation.
I hope this tutorial helped walk you through some basic visualization techniques.
It will be helpful to practice exploratory data analysis and visualization on a couple of different datasets, to understand what type of plot to use with different kinds of variables.
Data visualization is a great skill to have because you can help break down complex data and provide valuable insights based on your findings.
It not only helps you understand the data but also allows you to present it clearly to another person.