Visualizing Distributions
Learning Objectives
- You can distinguish a comparison of categories from a distribution of measurements.
- You can draw a histogram and explain which observations contribute to each interval.
- You can explain how changing the number of bins changes the display of the same data.
- You can use individual dots and boxplots to compare durations within categories.
- You can relate a boxplot’s median, quartiles, and whiskers to the original measurements.
The bar charts compared totals and mean durations between subjects. To see how individual session lengths vary, return to the original records. We will use three displays of those measurements: a histogram of all durations, individual dots separated by subject, and boxplots summarizing each subject’s distribution.
A distribution describes how measurements are spread across different values. A histogram displays a distribution by counting measurements within numeric intervals, called bins.
Counting Measurements in Intervals
Suppose an interval contains durations of at least 10 minutes but less than 30 minutes. A 15-minute session and a 25-minute session both contribute to its count. A 30-minute session belongs to the next interval.
With boundaries of 10, 30, 50, 70, 90, and 110 minutes, measurements fall into intervals as follows. Each interval includes its left boundary and excludes its right boundary, except the final interval, which includes both boundaries.
| Duration | Interval containing it |
|---|---|
| 25 minutes | At least 10, less than 30 minutes |
| 30 minutes | At least 30, less than 50 minutes |
| 50 minutes | At least 50, less than 70 minutes |
| 110 minutes | At least 90, at most 110 minutes |
An interval can have a count of zero if no recorded duration falls within it.
The histogram’s horizontal axis measures duration. Its vertical axis counts sessions. A bar height of eight therefore means eight sessions in that interval, not eight minutes of study.
Drawing a Histogram
Save this histogram example as analysis.py in data-analysis. It reads the original sessions.csv from Reading the Study Session CSV:
import pandas as pdimport seaborn as snsimport matplotlib.pyplot as plt
sessions = pd.read_csv("sessions.csv")
fig, ax = plt.subplots()sns.histplot(data=sessions, x="minutes", bins=5, ax=ax)ax.set_title("Study session lengths")ax.set_xlabel("Minutes per session")ax.set_ylabel("Number of sessions")fig.savefig("session-lengths.png", bbox_inches="tight")plt.close(fig)sns.histplot receives the original observations as data. The x argument selects the measured column. Unlike the category chart, this plot has no y column: the function calculates the counts.
bins=5 requests five equal-width intervals spanning the smallest and largest observed durations. Here, those values are 10 and 110, so each interval spans 20 minutes. The figure creation, labeling, saving, and closing follow the same pattern as the bar chart.
Run the script and open session-lengths.png. Compare it with Figure 1. Read one bar by identifying its horizontal interval first, then its height.

Figure 1. Five intervals summarize the individual session lengths.
For the complete sample, the counts are:
| Interval in minutes | Number of sessions |
|---|---|
| At least 10, less than 30 | 8 |
| At least 30, less than 50 | 10 |
| At least 50, less than 70 | 5 |
| At least 70, less than 90 | 0 |
| At least 90, at most 110 | 1 |
The counts sum to 24, the number of rows in the original table. The 110-minute session contributes one to the final count. Its duration does not become the bar’s height.
Use the interval boundaries and bar heights to interpret a histogram with different measurements:
Reading a Histogram
0 / 5 points
The histogram displays individual journey durations. Each interval includes its left boundary and excludes its right boundary, except the final interval, which includes 50 minutes.
Figure 1. Journey counts in four duration intervals.
How many journeys lasted at least 20 minutes but less than 40 minutes?
Choosing the Number of Bins
Change only bins=5 to bins=10 in analysis.py. Before running it, predict whether the number of observations changes. Then run the script and compare the saved image with Figure 2.

Figure 2. Ten narrower intervals display the same 24 measurements.
The interval from 10 to less than 30 minutes has split into two: three sessions are at least 10 but less than 20 minutes, and five are at least 20 but less than 30 minutes. Together, they account for the same eight sessions as before.
More bins show finer differences between nearby durations. Fewer bins combine more values into each count. Neither choice changes the CSV, the number of sessions, or the mean duration.
Individual Measurements and Grouped Results
The histogram shows that 23 of the 24 sessions lasted less than 70 minutes, while one lasted 110 minutes. It does not show which subject that long session belongs to. The grouped bar chart answers a different question by keeping subjects separate and adding their durations.
Choose the input table to match the question. For a distribution of session lengths, pass individual session records to histplot. Passing the four subject totals would instead show a distribution of four totals.
The next exercise supplies an Axes object with its labels already provided. Draw the individual journey durations on it and pass the function’s bins parameter to histplot.
A Histogram of Journey Durations
0 / 30 points
Complete draw_journey_histogram(journeys, bins, ax). Draw a histogram of the individual minutes values on the supplied ax, using the given number of bins. The starter already sets the title and axis labels. Keep those lines: the horizontal axis measures minutes, and the bar heights count journeys.
For durations 5, 10, 15, 20 and bins=2, each bin counts two journeys. Keep the complete bars and duration range visible, starting the count axis at zero.
The input is nonempty with numeric durations and no missing values. Leave journeys unchanged. The caller saves the Figure; your function only draws.
Showing Individual Durations by Subject
The histogram’s 110-minute observation could belong to any subject. A strip plot draws one dot for each observation and keeps the categories separate. With sessions still holding the original CSV, replace the histogram drawing block with:
fig, ax = plt.subplots()sns.stripplot(data=sessions, x="subject", y="minutes", jitter=True, ax=ax)ax.set_title("Individual study session lengths")ax.set_xlabel("Subject")ax.set_ylabel("Minutes per session")fig.savefig("session-dots.png", bbox_inches="tight")plt.close(fig) 
Figure 3. Each dot represents one recorded session; subjects appear in their order of first appearance in the CSV.
jitter=True moves dots slightly along the category axis so repeated durations are easier to see. Their vertical positions still show the recorded minutes. The small horizontal differences within one subject do not represent another measurement. The exact sideways positions can change between runs.
The three Writing dots expose both its small number of observations and the long session behind its 50-minute mean. Programming contributes ten dots, with durations from 20 to 50 minutes. For a table this small, keeping the measurements visible helps us check what the summaries describe.
Summarizing Each Distribution with a Boxplot
A boxplot uses the median and quartiles from Mean, Median, and Percentiles to summarize measurements within each category. The box extends from the 25th percentile, Q1, to the 75th percentile, Q3. A line inside marks the median. The box’s height is the interquartile range, IQR = Q3 - Q1.
Seaborn’s default whiskers extend to the most extreme recorded values within Q1 - 1.5 * IQR and Q3 + 1.5 * IQR. Where a whisker extends beyond the box, its end is an actual measurement, rather than necessarily one of those calculated limits. If no recorded value extends beyond a box edge within the limit, the whisker stays at that edge. Values beyond the whiskers appear as separate points, often called fliers. A point drawn separately is still an observation; that position alone does not establish a recording error.
Use the same original session records. Replace the strip plot’s drawing block with:
fig, ax = plt.subplots()sns.boxplot(data=sessions, x="subject", y="minutes", ax=ax)ax.set_title("Study session length distributions")ax.set_xlabel("Subject")ax.set_ylabel("Minutes per session")fig.savefig("session-boxplots.png", bbox_inches="tight")plt.close(fig) 
Figure 4. Boxplots summarize the same individual durations shown by the dots.
For Writing’s three durations, Q1 is 20, Q3 is 67.5, and the IQR is 47.5 minutes. The upper whisker limit is 67.5 + 1.5 * 47.5, or 138.75 minutes. Its 110-minute observation lies inside that limit, so it is the upper whisker end, rather than a separate flier. A long measurement need not appear as a flier within its own group.
The boxes have the same width even though the subjects have different numbers of sessions. A boxplot does not display every duration or establish how much evidence supports a comparison. Keep the individual dots or group counts available, especially for Writing’s three observations.
Pass raw records to boxplot, as with stripplot and histplot. Passing one total per subject would give each box only one combined value and lose the variation among sessions. The next exercise asks you to draw reading-duration boxplots on a supplied Axes. The caller handles the Figure, as in Drawing with a Function.
Reading Durations by Genre
0 / 35 points
Complete draw_reading_boxplots(readings, ax). Use seaborn to draw one vertical boxplot per genre on the supplied ax, using the raw minutes measurements. Genres may appear in any order. The starter provides the title and labels, including the duration unit; keep those lines.
Use the default quartiles and 1.5-IQR whiskers, showing measurements beyond the whiskers as separate points. Keep the complete boxplots and duration range visible. Do not replace individual readings with genre totals or averages.
The input is nonempty with genre names as strings, nonnegative numeric durations, and no missing values. Leave readings unchanged. The caller saves the Figure; your function only draws.
These charts describe how one measurement varies. The next chapter introduces charts for relationships between two measurements and changes over an ordered sequence.
Check Your Understanding
- What would a histogram of subject totals describe instead of a histogram of individual durations?
- The same observations are plotted with more bins. Why might some bars become shorter even though no duration has changed?
- Two dots for one subject have the same height but different horizontal positions. What does each position mean when the strip plot uses jitter?
- Why does Writing’s 110-minute session appear at a whisker end rather than as a separate point in its boxplot? Would a separate point establish that a value was recorded incorrectly?
- Can two boxes of the same width summarize different numbers of sessions? Which other display or calculation would reveal the group sizes?