Plotting a Quantitative Variable in Your Dataset
By: Marguerite Siboni
Tags: Python, Data Humanism, Comics, Data Science, Data Visualization

Borat and Wanda are quite alike. Borat likes to keep his look simple and tell stories as succinctly as possible. Wanda, on the other hand, likes to dress up a bit more. When she is explaining things, she spends a bit more time adding details to her stories. She wants her audience to understand a bit more about the anomalies she sees.

Box Plots in Practice

“Borat” shows a box plot, sometimes referred to as a box and whisker plot, in its simplest form. It gives you five pieces of information about a distribution: Max Value, Min Value, Median, 75% percentile and 25% percentile.

Note in the plot code below, the default parameters for the graph do not set the whiskers to max and min. To make this type of graph you must include “whis=’range’”.

plt.figure(figsize=(4, 8))

sns.boxplot(df.cal_per_cup, whis=’range’,

orient=’v’, color=’#BFD0FE’)

plt.ylabel(‘Calories Per Cup’)

plt.title(‘Distribution of Calories in Cereals’);

“Wanda” shows how box plots are more frequently used (sometimes referred to as Tukey box plots) and how you can adjust the parameters that you are showing to select what you are conveying. The default is to set the top whisker to show the largest point within the 1.5 Interquartile range (IQR). The IQR refers to the difference in value between the 25th and 75th percentile. This means that the top whisker is placed at the highest data point below 1.5 * the difference in the 25th and 75th percentiles above the 75th percentile. The bottom whisker is placed at the lowest point within 1.5*IQR. Any points outside of the whiskers are plotted as outliers.

plt.figure(figsize=(4, 8))

sns.boxplot(df.cal_per_cup,

orient=’v’, color=’#F6A6A0')

plt.ylabel(‘Calories Per Cup’)

plt.title(‘Distribution of Calories in Cereals’);

Box plots are great for conveying the spread and the skew of a dataset. If you would like to show several distributions and how they compare at once, box plots are a great way to do this. For example, below is the distribution of calories per cup of cereal divided by what shelf the cereal is on.

plt.figure(figsize=(15, 8))

sns.boxplot([‘Bottom Shelf’, ‘Middle Shelf’, ‘Top Shelf’],

[df[df.shelf==1].cal_per_cup, df[df.shelf==2].cal_per_cup, df[df.shelf==3].cal_per_cup],

orient=’v’, color=’#F6A6A0')

plt.ylabel(‘Calories Per Cup’)

plt.title(‘Distribution of Calories in Cereals by Shelf Placement’, fontsize=20);

By breaking the data up in to categories for the shelf placement, we can quickly see that all the outliers in the original box plot came from cereals on the top shelf. This shows that the top shelf not only has the highest median calorie count and the highest calorie cereals, it also includes the lowest calorie cereals.

The Son: Histogram Howard

After Borat and Wanda got together, they had three kids. One boy (Howard the Histogram) and twin girls named Viola and Violet (the violin plot pair).

Histogram Howard. He is a laid back guy. He likes to give you a visual display to draw your own conclusions from. He usually avoids the frills of smoothing his steps or displaying means, medians or percentiles. That said, when he’s feeling fancy, he does clean up nice with those details and more.

Howard can be a bit rough around the edges depending on what he’s showing and how many bins he chooses to show it in. He has a reputation of being lazy, as he’s usually the only one lying down. But every now and then he’ll stand up when it’s appropriate, like for the family photo above.

Histograms in Practice

A histogram will tell you a lot more about what a distribution shape is like — is it Gaussian? Is it uniform? Is it multi-modal? Is it skewed? Typically, a histogram does not make it immediately obvious where the median or quartiles are in a data set, although those can be added if desired.

When creating histograms, it’s important to think about how many bins you want as that will affect the clarity of your histogram and varies for all data sets (I often run a few graphs with different values set for the bin number to understand my data). In the code below, I set bins to equal to 5, then 20, then 50 to convey too few bins, a good number of bins and too many bins.

Note: for this data set, 20 bins conveyed the data well. However, there is no hard and fast rule for how many bins you should use and 20 could be bad for your data set. You should instead look at various numbers of bins and select the one that most clearly conveys the spread of values.