Everything You Need to Know About Histograms: Plotting in Python
Introduction
Histograms are one of the most common and useful ways to visualize the distribution of a dataset. At its core, a histogram shows how often different values occur within a sample by displaying the frequencies or counts of data points grouped into discrete intervals or "bins". This provides a quick and intuitive way to assess the shape, center, and spread of a distribution.
Histograms are particularly helpful for analyzing large datasets with many data points, where individual values would be impractical to plot. By binning the data into ranges, histograms summarize the overall patterns and trends. The height of each bar represents the count or relative frequency of values falling into that bin.
Not only are histograms a key tool for data exploration and analysis, but they are also widely used to communicate findings to others. Histograms are common in scientific publications, business reports, and data-driven journalism.
In this guide, we‘ll dive deep into histograms from a data science perspective. We‘ll cover when to use them, how to create them using Python‘s popular Matplotlib and Seaborn libraries, different styles and customization options, and some real-world examples and use cases. Let‘s get started!
When to Use Histograms
Histograms are most appropriate for visualizing the distribution of numerical data. This includes continuous quantities like heights, weights, temperatures, or test scores, as well as discrete counts or frequencies.
Some common use cases and insights from histograms include:
- Assessing the shape and symmetry of a distribution (is it uniform, normal, skewed, bimodal, etc.?)
- Identifying where the data is centered (what‘s the typical or most frequent value?)
- Determining the spread and variability of the data (is it closely clustered or widely dispersed?)
- Spotting potential outliers that fall far outside the main distribution
- Comparing multiple distributions side-by-side
- Examining changes in a distribution over time or across different groups
In general, if you have numerical data and want to understand its overall characteristics, a histogram is a great place to start. They provide a bird‘s eye view of the data that can guide future analyses and modeling decisions.
Plotting Histograms in Python
Python has many excellent libraries for data visualization, but Matplotlib is the foundation that most others are built upon. It provides a MATLAB-style interface for creating a wide range of static, animated, and interactive plots.
To plot a histogram in Matplotlib, we‘ll use the pyplot.hist() function. But first, we need to import the required libraries and load our data into a suitable format like a NumPy array or Pandas Series.
import numpy as np
import matplotlib.pyplot as plt
# Generate some random data
data = np.random.normal(loc=50, scale=10, size=1000)
# Plot histogram
plt.hist(data)
plt.xlabel(‘Value‘)
plt.ylabel(‘Frequency‘)
plt.title(‘Histogram of Random Data‘)
plt.show()
This code generates a sample of 1000 values from a normal distribution with mean 50 and standard deviation 10, then plots them as a histogram. By default, Matplotlib automatically selects the number and ranges of the bins.
However, we can easily customize our plot by adjusting the arguments to plt.hist():
- bins: The number of bins (default is 10) or a list of bin edges
- range: The lower and upper range of the bins
- density: If True, plot as a probability density (i.e. normalize the area under the curve to 1)
- histtype: The type of plot (‘bar‘, ‘step‘, ‘stepfilled‘)
- rwidth: The relative width of the bars (between 0 and 1)
- color: The fill color of the bars
- edgecolor: The edge color of the bars
- alpha: The transparency of the plot (between 0 and 1)
We can also plot multiple histograms on the same axis for comparison:
data1 = np.random.normal(50, 5, 1000)
data2 = np.random.normal(55, 10, 1000)
plt.hist(data1, alpha=0.5, label=‘Data 1‘)
plt.hist(data2, alpha=0.5, label=‘Data 2‘)
plt.legend(loc=‘upper right‘)
plt.show()
In addition to Matplotlib, the Seaborn library provides a higher-level interface for statistical data visualization. It has several built-in themes and color palettes to enhance the aesthetic appeal of plots.
To create a histogram in Seaborn, we can use the distplot() function:
import seaborn as sns
sns.distplot(data, hist=True, kde=False,
bins=20, color = ‘darkblue‘,
hist_kws={‘edgecolor‘:‘black‘},
kde_kws={‘linewidth‘: 4})
This offers even more options for customization, such as adding a kernel density estimate (KDE) curve, controlling the bandwidth of the KDE, and setting the number and style of tick marks.
Advanced Histogram Techniques
Beyond the basic histogram, there are several variations and related plots that can provide additional insights:
-
Normalized or probability density histograms show the relative frequencies as percentages or probabilities instead of raw counts. This is useful for comparing distributions with different sample sizes.
-
Cumulative histograms plot the cumulative counts or frequencies at each bin. This shows how many or what fraction of data points fall below a given value.
-
For bivariate data (i.e. two paired variables), we can use a 2D histogram or hexagonal bin plot. These show the joint distribution and correlation between the variables.
-
Kernel density estimation (KDE) plots are smoothed versions of histograms that estimate the underlying probability density function. These are less sensitive to the choice of bin size and can work better for small datasets.
Here‘s an example of plotting a 2D histogram using Matplotlib:
x = np.random.normal(5, 2, 1000)
y = x * 3 + np.random.normal(0, 2, 1000)
plt.hist2d(x, y, bins=30, cmap=‘Blues‘)
cb = plt.colorbar()
cb.set_label(‘counts in bin‘)
And a hexagonal bin plot using Seaborn:
x = np.random.normal(50, 10, 1000)
y = np.random.normal(50, 10, 1000)
sns.jointplot(x, y, kind="hex", color="b")
Real-World Applications
Histograms have countless applications across various domains of science, engineering, business, and more. Here are just a few examples:
-
In education, histograms can display the distribution of student grades on an exam, revealing the difficulty of the test and the performance of the class. Teachers can also compare grade distributions across different classes, schools, or years.
-
In manufacturing, histograms of product measurements (e.g. weights, dimensions) can show if a process is meeting quality control specifications. Monitoring histograms over time can detect changes or drift in the production line.
-
In image processing and computer vision, histograms of pixel intensities or colors are used for segmentation, thresholding, and object detection. Comparing image histograms can assess the similarity between two images.
-
In medical research, histograms of patient metrics like blood pressure, cholesterol levels, or disease severity can identify high-risk subgroups. Overlaying patient data with population averages can highlight individuals needing intervention.
Tips for Effective Histograms
To make the most informative and compelling histograms, keep these tips in mind:
-
Choose an appropriate bin size. Too few bins will obscure important details, while too many will overemphasize random noise. Consider the sample size, data range, and desired resolution when setting the number or width of bins.
-
Start the y-axis at zero. This avoids exaggerating small differences between bars. Use a broken or truncated axis only if there‘s a compelling reason.
-
Directly label the bars with counts or percentages. This reduces the cognitive load for your audience and highlights the key takeaways.
-
Add clear titles and axis labels. Explain what the histogram shows and include the units of the quantities. Avoid acronyms or jargon that may be unfamiliar to your readers.
-
Use annotations, reference lines, or different colors to draw attention to important features. This could include outliers, thresholds, or significant subgroups in the data.
-
Consider transforming skewed data. Applying a log transform can make highly skewed distributions easier to visualize and interpret. Note the transform in the axis labels or legend.
-
Keep it simple. Minimize chart junk like unnecessary gridlines, borders, or 3D effects. Let the data speak for itself.
Histogram FAQs
Q: What‘s the difference between a histogram and a bar chart?
A: A histogram displays the distribution of a numeric variable, with the bars representing counts or frequencies of data within successive intervals. A bar chart compares categorical variables, with the bars showing a measured quantity for each category.
Q: How do you interpret a histogram?
A: Look at the overall shape to see if the distribution is symmetric, skewed, unimodal, bimodal, etc. Identify where the peak or center of the data lies. Check the spread of the data from the width of the distribution. Note any gaps or outliers in the tails.
Q: What are some common problems or mistakes with histograms?
A: Choosing the wrong bin size can distort the true shape of the data. Plotting counts instead of density makes it hard to compare distributions with different sample sizes. Forgetting to label the axes or explain the units can confuse the audience.
Conclusion
Histograms are a fundamental tool for visualizing and understanding the distribution of numerical data. We‘ve covered when to use them, how to create them in Python, different styles and variations, real-world applications, and tips for effective presentation.
Of course, this is just a starting point. There are many more advanced techniques and libraries for data visualization in Python, such as Plotly, Bokeh, Altair, and D3.js. But mastering the humble histogram with Matplotlib and Seaborn will take you far.
The key is to always keep your audience and message in mind. What does the data say and why does it matter? Histograms are not just pretty pictures – they are powerful tools for communicating insights and driving decisions.
I hope this guide has boosted your understanding and appreciation of histograms. For more tips and techniques, check out the excellent Data Visualization section of the Python Data Science Handbook by Jake VanderPlas. Happy plotting!