A Practical Guide to Data Visualization | by Soner Yıldırım | Towards Data Science

We live in the era of big data. We can collect lots of data which allows to infer meaningful results and make informed business decisions. However, as the amount of data increases, it gets trickier to analyze and explore the data. There comes in the power of visualizations which are great tools in exploratory data analysis when used efficiently and appropriately. Visualizations also help to deliver a message to your audience or inform them about your findings. There is no one-fits-all kind of visualization method so certain tasks require different kinds of visualizations. In this post, we will cover how to create basic plots and efficiently use them.
We need a sample dataframe to work on. In this post, we will use two different datasets both of which are available on Kaggle. First one is telco customer churn dataset and the other one is US cars dataset.
import pandas as pdimport numpy as npdf = pd.read_csv("Projects/churn_prediction/Telco-Customer-Churn.csv")df.shape(7043, 21)
The dataset includes 21 columns. “Churn” column indicates whether a customer has churned (i.e. left the company) and remaining columns include information about the customer or the products that customer have.
Note: There are many tools and software packages to create great visualizations. In this post, I will use two of the most common ones which are matplotlib and seaborn. Feel free to use any package as long as you get what you want.
import matplotlib.pyplot as pltimport seaborn as snssns.set(style="darkgrid")%matplotlib inline
%matplotlib inline command allows rendering the figures in the notebook so we can see them instantly.
Before starting on creating visualizations, I would like to emphasize a point. The main goal of visualizing data is to explore and analyze the data or interpret the results and findings. Of course, we need to pay attention to how the figures look and try to create appealing figures. However, very beautiful visualizations without any informative power are useless in data analysis. Let’s start with keeping this point in mind.
The main object of this dataset is customer churn. So, it is better to check how this variable looks:
plt.figure(figsize=(8,5))sns.countplot('Churn', data=df)
We created a figure object with a specified size with matplotlib backend. Then, added a countplot using seaborn. This figure obviously tells us the company is good at keeping its customers because churn rate is actually low.
This figure is plain and simple. Let’s add some informative power to it. We can see how churn changes depending on “SeniorCitizen” and “gender” columns:
sns.catplot('Churn', hue='SeniorCitizen',             col='gender', kind='count',             height=4, aspect=1, data=df)
Gender seems to be not changing the churn rate but there is a difference between senior and non-senior citizens. Senior citizens are more likely to churn. We can expand our analysis by trying other columns in this way.
Another way to explore data is to check the distributions of variables which give us an idea about the spread and density. Let’s check it on “tenure” and “MonthlyCharges” features.
fig, axs = plt.subplots(ncols=2, figsize=(10,6))sns.distplot(df.tenure, ax=axs[0]).set_title("Distribution of Tenure")sns.distplot(df.MonthlyCharges, ax=axs[1]).set_title("Distribution of MonthlyCharges")
We created the figure object with two subplots. Then, created distribution plots using seaborn. We also added titles using set_title:
Tenure variable indicates how long a customer has been a customer in months. Most of the customers are pretty new or have been a customer for a long time. MonthlyCharges variable exhibits a strange distribution but the high density is visible on the lowest amount.
Another way to have an idea about the dispersion of data is boxplot.
plt.figure(figsize=(10,6))sns.boxplot(x="Contract", y="MonthlyCharges", data=df)
The line in the box represents the median. The lower and upper edges of the boxes show first and third quantile, respectively. So, tall boxes indicate the values are more spread out. What we can understand from this plot:
Short-term contracts have a smaller price range
As the contract period increases, monthly charges tend to decrease
The second dataset is different with respect to the target variable. Churn is a categorical variable which makes the task a classification whereas price is continous and we need to solve a regression problem to predict price.
After dropping redundant columns, cars dataset looks like:
Let’s see how mileage and price are correlated:
sns.relplot(x="price", y="mileage", hue="brand", data=df,           height=6, aspect=1.2)
As expected, there is a negative correlation between price and mileage. Another thing to spot in this plot is “nissan” cars. They are located in the low price — low mileage region compared to other brands.
Let’s check the distribution of prices:
plt.figure(figsize=(10,6))sns.distplot(df['price']).set_title('Distribution of Car Prices')
The price variable is not normally distributed. It is right-skewed meaning the tail of the distribution is longer on the right side than on the left side. Another way to prove skewness is by comparing mean and median. If the mean is higher than the median, the distribution is right-skewed. Mean is the average value and median is the value in the middle. So if mean is higher than the median, we have more samples on the upper side of the median.
Mean is higher than median, as expected. So, there are more of expensive cars than of cheaper cars.
Now I want to show two kind-of-related visualization types: scatter plot and point plot. Scatter plot shows the relationship between two sets of data by scattering the datapoint. Point plot shows point estimates and confidence intervals. So, it is kind of an estimation based on scatter plot. As stated in seaborn documentation, “a point plot represents an estimate of central tendency for a numeric variable by the position of scatter plot points and provides some indication of the uncertainty around that estimate using error bars”. Let’s go over an example to make the concept more clear:
#Scatter plotplt.figure(figsize=(12,8))sns.scatterplot(x="age", y="mileage", data=df).set_title("Scatter plot")#Point plotplt.figure(figsize=(12,8))sns.pointplot(x="age", y="mileage", data=df).set_title("Point plot")
The length of bars on point plot is proportional to the uncertainty. If we compare two plots, we see that bars are shorter in the areas in which scatter plot points are dense.
There are many more different visualization types. Depending on the task, different options may be more suitable. There are also dynamic graphs which are usually better options for time series analysis. What all visualizations have in common is that they are great tools for exploratory data analysis and the storytelling part of data science.
A Practical Guide for Exploratory Data Analysis
A Complete Pandas Guide
Machine Learning Guide — Learning by Doing
Thank you for reading. Please let me know if you have any questions.