Learning data visualization differently
By: Mahbubul Alam
Tags: Seaborn, Visualization, Machine Learning, Data Visualization, Data Science

Visualizing a single variable

As I just said, visualization of a SINGLE variable tells us how it is distributed with respect to the central tendency and dispersion (e.g. mean, median, quartiles etc.).

We’ll pick the variable total_bill and see its distribution using different visualization techniques. In a way, all these techniques present similar kinds of information, with just some variation in presentation.

I’m not going to explain too much, as these figures are self-explanatory, it should be quite easy for readers to examine codes and compare similarities and differences between the plots.

Histogram

sns.distplot(df["total_bill"], kde=False)

KDE plot

sns.kdeplot(df["total_bill"])

Boxplot

sns.boxplot(df["total_bill"])

Violin plot

sns.violinplot(df["total_bill"])

Visualizing two variables

In a two-dimensional space, it’s easy to visualize two variables that people can easily comprehend. These two variables can be any combination of continuous, categorical and integer data types.

Scatterplot and linear models are best represented with continuous variables and that’s what the first two figures below are about. Boxplot, violin plot, swarm plot and bar plots are great for presenting categorical variables with continuous and integer data types.

Scatter diagram

A scatterplot shows how one variable is distributed with respect to another variable and is the simplest form of bivariate plots that immediately reveal valuable information that is also visible to “nacked eyes”.

sns.scatterplot(x="total_bill", y='tip', data=df)

Linear model

Linear model formalizes the statistical relationship between two variables presented in a scatterplot with associated confidence intervals.

sns.lmplot(x="total_bill", y="tip", data=df)

Boxplot

Boxplots can compare categorical variables and their distribution on a continuous variable. Here I’ve disaggregated tip amount by the time of restaurant visits, and as you can tell, people are more generous during dinner time compared to lunch visits. The variation is also higher during dinner visits with much more outliers values.

sns.boxplot(x='time', y='tip', data=df)

Boxplot [people are generous at dinner!!!]

Swarm plot

Boxplot above is great, but swarm plots can show each data point and their distribution. You lose some statistical information as in boxplots but you gain something else! So they can be complementary during exploratory data analysis (EDA).

sns.swarmplot(x='time', y='tip', data=df)

Violin plot

Violin plot smooths out “rough edges” of swarm plot but in this case, you cannot see the individual data points. However, you may notice that there is a boxplot inside each of the violin plots — which certainly adds value.

sns.violinplot(x='time', y='tip', data=df)

Barplot

Barplot is an outdated visualization technique with little information, but I’m including it here anyway so you can compare the differences with other plots at a glance.

sns.barplot(x='time', y='tip', data=df)

3) Visualizing multiple variables

In two-dimensional plots, you really don’t have that many options to visualize multi-dimensional values. Multivariate plots are nothing but a representation of one or more additional variables in bivariate plots.

So basically, multivariate plots take 2D plots one step further by adding variables via parameters such as hue, style, bubble size etc. Although, in theory, you can represent more variables, but it can quickly become crowded and difficult to read as we will see shortly.

Let’s examine some plots below.

Scatterplot with hue (color)

In the scatterplot, we can separate different categories (e.g. male and female) with different shades of color (called the “hue” parameter).

So here we have 3 variables: tip, total_bill and sex represented.

sns.scatterplot(x="total_bill", y='tip', data=df, hue='sex')

Scatterplot with hue + style

On top of the above plot, we could add another variable separated by the “style” parameter.

Now we have 4 variables: tip, total_bill, sex, visit time.

Great, but how are you able to track & interpret all that information in a single 2D plot?

sns.scatterplot(x="total_bill", y='tip', data=df, hue='sex', style='time')

Scatterplot with hue + style + size

We can get even crazier and add a “size” parameter for yet another variable. We can go on, but at this point you must be lost if you are trying to interpret what this plot is all about!

The point I am trying to make is this — just because you can, you shouldn't try to accomplish too many things in a single visualization. Beyond a certain point it becomes junk! As I said in the beginning — simplicity matters.

But there are legitimate ways to show multiple variables and for that, we can go back to our boxplot, violin plot and swarm plot. They work perfectly in a 2D plot to represent multiple variables.

Boxplot

sns.boxplot(x='time', y='tip', data=df, hue='sex')

Violin plot

sns.violinplot(x='time', y='tip', data=df, hue='sex', split=True)

Swarmplot

sns.swarmplot(x='time', y='tip', data=df, hue='sex')

I could go on but you get the idea.

Summary and final word

I touched upon a few different things, so I felt I need to summarise them. First, simplicity matters; it puts less pressure on the audience and forces the creators to explain complex information in a digestible way.

Second, when you are learning visualization, you should first focus on how to communicate information using visualization as a medium. Styles and aesthetics will come naturally later on but that’s the cherry on top. It’s also easy to get lost when your one-liner code becomes a complex one.

And finally, adding more variables in the plot is possible, but may not be desirable, as the figure quickly becomes crowded with too much information that is difficult to interpret. The purpose of data visualization is communication, so the simpler the better.

I hope this was a useful post. If you have comments feel free to write them down below. You can follow me on Medium, Twitter or LinkedIn.

Happy plotting!