Creating an Elegant Plot
By: Andrew Ozbun
Tags: Data Visualization, Seaborn, Eda, Matplotlib

Creating visualizations for you data is essential. In another post I did I take an in depth look at EDA according to the National Institute of Standards of Technology which can be found here.

After talking about the importance of EDA, it become a syntactical issue. In this article I plan walking through different techniques and tricks for customizing plots in Python.

As you read, keep referring back to this table I created for myself in an intro to Stats class. Actually my professor created it as a list, but I made a table out of it and keep it posted on the bulletin board in my office. It has proved to be invaluable. The only caveat to this is that the more and more I go through my data journey I hear and read people say “Oh god, never use a pie chart”. I have to agree on this tiny little detail. I have rarely see a pie chart used in a professional study. After creating a ton of my own visualizations, I personally dislike using them as well. They just aren’t the most effective way to convey the story you want to tell.

Taking a second to think abut the variable types used leads to optimal visualization.

Basic Charts and Graphs

Matplotlib and Seaborn will be the two most quintessential libraries used by a data scientist or analyst in Python.

Matplotlib

Matplotlib is the gold standard for creating any sort of statistical visualization, based on the infamous MATPLOTLAB. An extension of Numpy, its simple object oriented programming can be embedded easily into applications and GUIs. It does have a rudimentary feel to it, making matplotlib alone appear less refined than adding additional libraries. Below is a simple bar plot in matplotlib just showing class imbalance, more likely used for technical analysis than showing a stakeholder.[1]

Seaborn

Seaborn is a statistical graphics library that builds on top of matplotlib and pandas data frames. Even basic seaborn plots can add a little more sophistication to a visualization. It is important to note when working with seaborn that the sns call can be used in tandem with the plt call. Below is a basic seaborn barplot call with plt.show() for cleaning up the output. Also note the ability to call color palettes as opposed to basic colors.

Seaborn Palettes

Seaborn is infamous for their wide array of predetermined palettes, which tend to look seamless and pleasing. Coming in a wide variety of color spectrums, this is a feature that makes seaborn highly customizable. If you take a look at the seaborn documentation, there are arguments that you can add to the palette call that will customize your graph even further.

Created in google colab, these are some of my favorite palettes I like to use.

A Few Odd Graphs

Histogram

When first learning statistics I could not figure out the difference between a histogram and a bar chart. I think the simplest way to put it is that a histogram measures the frequency of continuous variable and a bar chart measures the frequency of a categorical variable. The graph below was used to check for a normal distribution in housing prices.

Histogram of Housing Prices

Bubble Plot

A bubble plot is used to describe three quantitative or continuous variables. I find that they are particularly useful when it is difficult to understand the visualization from looking at just two variables alone. Adding a tertiary variable always helps. Look at the bottom of the graph and the color and size of the smaller pink bubbles. They start at the bottom and fizzle out at the top and curve. In contrast the large purple bubbles have a high concentration at the top and scoop downward underneath the majority of the visualization.

Why is all this? The answer I came up with is that although homes built before 1940 are smaller they maintain a higher resale value. Newer homes have far more space but don’t seem to hold their value.

Labelling and Refining

The example bar chart that I am using for this section is a segmented bar chart which is best for two categorical variables. In our case a tertiary variable was shown. This is based on foreign and total world wide grosses for movies. When the foreign amount is stacked on the world wide gross, the remaining section of the bar becomes the domestic gross. This results in our three continuous variables: foreign gross, domestic gross, and total world wide gross. Each has a valuable story to tell.

You can achieve this by not designating the two different bar charts to different axes calls. Its important to note however, that when you do this there will be specifications like both bar charts needing to have the same features in order to appease matplotlib.