Miscellany / Statistics and analysis
Misleading Box Plots Limitations and Alternatives
Contents
- Understanding Box Plots
- Source Code for Understanding Box Plots
- Results for Understanding Box Plots
- Limitations of Box Plots
- Source Code for Limitations of Box Plots
- Results for Limitations of Box Plots
- The Importance of Data Distribution
- Source Code for The Importance of Data Distribution
- Results for The Importance of Data Distribution
- Complementary Visualizations
- Source Code for Complementary Visualizations
- Results for Complementary Visualizations
- Real-Life Example: Climate Data Analysis
- Source Code for Climate Data Analysis
- Results for Climate Data Analysis
- Real-Life Example: Student Performance Analysis
- Source Code for Student Performance Analysis
- Results for Student Performance Analysis
- Conclusion
- Additional Resources
Understanding Box Plots
Box plots are powerful tools for visualizing data distributions, but they can sometimes be misleading. A box plot represents only five key values: the minimum, first quartile, median, third quartile, and maximum. While this simplification is often useful, it can obscure important details about the underlying data distribution.
Source Code for Understanding Box Plots
import random
import matplotlib.pyplot as plt
# Generate three different datasets
data1 = [random.gauss(0, 1) for _ in range(1000)]
data2 = [random.expovariate(1) for _ in range(1000)]
data3 = [random.uniform(-2, 2) for _ in range(1000)]
# Create box plots
fig, ax = plt.subplots(figsize=(10, 6))
ax.boxplot([data1, data2, data3], labels=['Normal', 'Exponential', 'Uniform'])
ax.set_title('Box Plots of Different Distributions')
ax.set_ylabel('Values')
plt.show()
Results for Understanding Box Plots
# Output:
# A plot showing three box plots side by side, representing the Normal,
# Exponential, and Uniform distributions. Despite their different shapes,
# the box plots may appear similar, highlighting the potential for
# misinterpretation.
Limitations of Box Plots
While box plots provide a concise summary of data, they can mask important differences between distributions. Two datasets with vastly different shapes may produce similar box plots if their key statistics align. This limitation can lead to incorrect conclusions if box plots are used as the sole method of data visualization.
Source Code for Limitations of Box Plots
import numpy as np
import matplotlib.pyplot as plt
# Generate two datasets with different distributions but similar statistics
np.random.seed(42)
data1 = np.concatenate([np.random.normal(0, 1, 1000), np.random.normal(5, 1, 1000)])
data2 = np.random.uniform(-2, 7, 2000)
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Plot histograms and box plots
ax1.hist(data1, bins=30, alpha=0.7)
ax1.boxplot(data1, vert=False, positions=[2])
ax1.set_title('Bimodal Distribution')
ax2.hist(data2, bins=30, alpha=0.7)
ax2.boxplot(data2, vert=False, positions=[2])
ax2.set_title('Uniform Distribution')
plt.tight_layout()
plt.show()
Results for Limitations of Box Plots
# Output:
# A figure with two subplots:
# 1. A histogram of a bimodal distribution with its box plot
# 2. A histogram of a uniform distribution with its box plot
# The box plots look similar despite the very different underlying distributions.
The Importance of Data Distribution
To avoid being misled by box plots, it's crucial to examine the underlying data distribution. Histograms, density plots, or scatter plots can provide more detailed information about the shape and characteristics of the data. These visualizations can reveal important features such as multimodality, skewness, or outliers that may not be apparent from a box plot alone.
Source Code for The Importance of Data Distribution
import numpy as np
import matplotlib.pyplot as plt
# Generate a dataset with a complex distribution
np.random.seed(42)
data = np.concatenate([
np.random.normal(-2, 0.5, 1000),
np.random.normal(2, 0.5, 1000),
np.random.normal(0, 0.2, 500)
])
# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
# Plot box plot
ax1.boxplot(data)
ax1.set_title('Box Plot')
# Plot histogram
ax2.hist(data, bins=50)
ax2.set_title('Histogram')
plt.tight_layout()
plt.show()
Results for The Importance of Data Distribution
# Output:
# A figure with two subplots:
# 1. A box plot of the complex distribution
# 2. A histogram of the same distribution
# The histogram reveals the trimodal nature of the data,
# which is not apparent from the box plot alone.
Complementary Visualizations
To get a complete picture of your data, it's best to use box plots in conjunction with other visualizations. Violin plots, for example, combine the simplicity of box plots with the detail of density plots. Swarm plots can show individual data points, providing a clear view of the data's distribution and potential outliers.
Source Code for Complementary Visualizations
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Generate dataset
np.random.seed(42)
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
# Create figure
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))
# Box plot
sns.boxplot(data=data, ax=ax1)
ax1.set_title('Box Plot')
# Violin plot
sns.violinplot(data=data, ax=ax2)
ax2.set_title('Violin Plot')
# Swarm plot
sns.swarmplot(data=data, ax=ax3)
ax3.set_title('Swarm Plot')
plt.tight_layout()
plt.show()
Results for Complementary Visualizations
# Output:
# A figure with three subplots:
# 1. A box plot of three distributions
# 2. A violin plot of the same distributions
# 3. A swarm plot of the same distributions
# The violin and swarm plots provide more detailed information about
# the shape and spread of the data compared to the box plot.
Real-Life Example: Climate Data Analysis
In climate science, box plots are often used to compare temperature distributions across different regions or time periods. However, relying solely on box plots could mask important patterns such as seasonal variations or long-term trends. Let's examine a hypothetical dataset of monthly temperatures for a city over a year.
Source Code for Climate Data Analysis
import numpy as np
import matplotlib.pyplot as plt
# Generate synthetic temperature data
np.random.seed(42)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
base_temp = np.array([0, 2, 7, 12, 17, 20, 22, 21, 17, 11, 6, 2])
temp_data = [base_temp[i] + np.random.normal(0, 2, 30) for i in range(12)]
# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10))
# Box plot
ax1.boxplot(temp_data, labels=months)
ax1.set_title('Monthly Temperature Distribution (Box Plot)')
ax1.set_ylabel('Temperature (°C)')
# Line plot
for i, month_data in enumerate(temp_data):
ax2.plot(range(1, 31), month_data, label=months[i], alpha=0.7)
ax2.set_title('Monthly Temperature Trends (Line Plot)')
ax2.set_xlabel('Day of Month')
ax2.set_ylabel('Temperature (°C)')
ax2.legend(ncol=4, loc='upper center', bbox_to_anchor=(0.5, -0.15))
plt.tight_layout()
plt.show()
Results for Climate Data Analysis
# Output:
# A figure with two subplots:
# 1. A box plot showing temperature distributions for each month
# 2. A line plot showing daily temperature trends for each month
# The line plot reveals day-to-day variations and potential outliers
# that are not visible in the box plot.
Real-Life Example: Student Performance Analysis
In educational settings, box plots are frequently used to compare student performance across different subjects or classes. However, this approach might overlook important patterns in score distribution. Let's examine a hypothetical dataset of student scores in various subjects.
Source Code for Student Performance Analysis
import numpy as np
import matplotlib.pyplot as plt
# Generate synthetic student score data
np.random.seed(42)
subjects = ['Math', 'Science', 'English', 'History', 'Art']
scores = [
np.concatenate([np.random.normal(70, 10, 80), np.random.normal(90, 5, 20)]), # Math
np.random.normal(75, 15, 100), # Science
np.random.normal(80, 10, 100), # English
np.random.beta(7, 3, 100) * 100, # History
np.concatenate([np.random.normal(60, 15, 50), np.random.normal(90, 5, 50)]) # Art
]
# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10))
# Box plot
ax1.boxplot(scores, labels=subjects)
ax1.set_title('Student Scores by Subject (Box Plot)')
ax1.set_ylabel('Score')
# Violin plot
parts = ax2.violinplot(scores, showmeans=True, showextrema=True, showmedians=True)
ax2.set_title('Student Scores by Subject (Violin Plot)')
ax2.set_xticks(range(1, len(subjects) + 1))
ax2.set_xticklabels(subjects)
ax2.set_ylabel('Score')
# Customize violin plot colors
for pc in parts['bodies']:
pc.set_facecolor('lightblue')
pc.set_edgecolor('black')
plt.tight_layout()
plt.show()
Results for Student Performance Analysis
# Output:
# A figure with two subplots:
# 1. A box plot showing score distributions for each subject
# 2. A violin plot showing more detailed score distributions
# The violin plot reveals bimodal distributions in Math and Art,
# and a skewed distribution in History, which are not apparent
# from the box plot alone.
Conclusion
While box plots are valuable tools for data visualization, they should not be used in isolation. To avoid misleading interpretations, always consider the underlying data distribution and use complementary visualizations. By combining box plots with other techniques such as histograms, density plots, or violin plots, you can gain a more comprehensive understanding of your data and make more informed decisions.
Additional Resources
For further exploration of data visualization techniques and their applications:
- Wickham, H., & Grolemund, G. (2016). R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O'Reilly Media.
- Tufte, E. R. (2001). The Visual Display of Quantitative Information (2nd ed.). Graphics Press.
- ArXiv paper: "Beyond Bar and Line Graphs: Time for a New Data Presentation Paradigm" by Weissgerber et al. (2015) URL: https://arxiv.org/abs/1503.02767
- ArXiv paper: "Visualization of Uncertainty in Probabilistic Forecasts" by Kinkeldey et al. (2020) URL: https://arxiv.org/abs/2010.07167