Practice: Python, the Inquiry Tool and Reading Graphs
Histograms with two bin widths, boxplots and violin plots by group, and a frequency table with conditional rates in pandas and matplotlib, code-reading questions, practice with the inquiry tool and the interpretation of a graph, on the 2026 Stack Overflow Developer Survey, and two decisions from real hospital data.
Last updated:
On this practice page we run more analyses of the 2026 Stack Overflow Developer Survey in Python, with the commands of Same Statistics, Different Pictures and Comparing and Relating:
a histogram with two bin widths, boxplots and violin plots by group, and a frequency table with conditional rates for age and the use of artificial intelligence (AI) tools.
Each program is followed by a question on similar code: what the program computes.
Then we work with the inquiry tool “Same statistics, different picture”, interpret a graph of salaries, and make decisions from real hospital lengths of stay.
The data: the reduced file, under the Open Database License:
Stack Overflow Developer Survey 2026, Stack Exchange, Inc. (licence details).
A histogram with two bin widths
The survey asked: “Including any education, how many years have you been coding in total?”.
The answers are in the column years_code.
Think: we will draw a histogram of the answers with bins one year wide.
Would you expect some bars to be much taller than their neighbours?
At which numbers, and why?
The method dropna() returns the column without its missing values:
those who did not answer the question. The other commands appear in Same Statistics, Different Pictures:np.arange(0, 51, 1) gives the bin edges 0, 1, 2 up to 50, and np.arange(0, 55, 5) the edges 0, 5, 10 up to 50.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('so-2026-week1.csv')
years = df['years_code'].dropna()
print(len(years), years.median(), round(years.mean(), 2))
plt.hist(years, bins=np.arange(0, 51, 1))
plt.xlabel('years of coding (bins of 1 year)')
plt.show()
25204 15.0 17.33
plt.hist(years, bins=np.arange(0, 55, 5))
plt.xlabel('years of coding (bins of 5 years)')
plt.show()
Question 1. Compare the two histograms. Where is the tallest bar in each?
What do the one-year bins show that the five-year bins do not, and what causes it?
Solution
With one-year bins the tallest bar is at 10 years (1,839 answers), and there are tall bars at every multiple of 5:
15 (1,414), 20 (1,594), 25 (1,050), 30 (1,108). Their neighbours are much lower:
9 and 11 years have 679 and 578, 19 and 21 have 243 and 279.
As with the number of tools in Comparing and Relating, these are rounded answers:
people who have been coding “for about 20 years” write 20.
With five-year bins the tallest bar is 5 to 10 (5,021 answers), not 10 to 15 (4,821), even though the one-year bins peak at 10.
Each wide bar counts five values together, and the spikes at the round numbers are absorbed into it:
the shape looks smooth, with one peak and a right tail. Both pictures are correct; they answer different questions.
The narrow bins show how people answer, and the wide ones the overall shape.
Neither picture shows the 216 answers above 50.
What does this program compute?
counts, edges, bars = plt.hist(years, bins=np.arange(0, 51, 1))
print(counts[10] / counts[9])
Answer
counts[10] is the number of answers in the bar at position 10 (counting from zero), the range from 10 to 11:
the answers “10 years”. counts[9] is the number of answers “9 years”.
The program prints how many times more respondents answered 10 than 9:
.
Boxplots and violin plots by age group
We compare the years of coding in four age groups, from 18 to 54.
The survey asked for age in groups, so it is a categorical variable.
Think: how will the median and the IQR of the years of coding change from one age group to the next?
Where might the violin plot show something that the boxplot does not?
As in the histograms, we keep answers of up to 50 years: there are 216 answers above it, and some of them are impossible (the age group 18–24 has answers of 100 years).
The condition df[‘years_code’] <= 50 is True in the rows whose value is at most 50 (<= is “less than or equal to”), and a missing value never meets it.
The program builds a list of four columns, one for each age group, and draws them with plt.boxplot and plt.violinplot.plt.xticks writes a name under each group. At the end, zip(names, groups) goes over the two lists side by side:
in each round of the loop, the name of a group and its column.
ages = ['18-24 years old', '25-34 years old',
'35-44 years old', '45-54 years old']
names = ['18-24', '25-34', '35-44', '45-54']
d = df[df['years_code'] <= 50]
groups = [d[d['age_group'] == a]['years_code'] for a in ages]
plt.boxplot(groups)
plt.xticks([1, 2, 3, 4], names)
plt.show()
for name, g in zip(names, groups):
print(name, len(g), g.median())
18-24 3650 6.0
25-34 7397 10.0
35-44 7226 20.0
45-54 3690 30.0
plt.violinplot(groups, showmedians=True)
plt.xticks([1, 2, 3, 4], names)
plt.show()
Question 2. What changes between the age groups in the boxplot?
What does the violin plot show in the groups 35–44 and 45–54 that the boxplot does not?
Solution
The median rises from 6 years in the youngest group to 10, 20 and 30, and the boxes widen:
the IQR (by the pandas method) is 4, 6, 9 and 11.75 years. Part of the widening may come from the groups themselves:
the group 18–24 spans 7 years of age and the others 10, and the older a group, the wider the range of possible years of coding.
In the violin plots of 35–44 and 45–54 there are humps at the round numbers:
in the group 35–44 the most common answers are 20 (1,063), 25 (613) and 15 (601), and in the group 45–54 30 (634), 25 (344) and 20 (263).
The boxplot summarises each group in five numbers, so the humps do not show in it.
What does this program compute?
for name, g in zip(names, groups):
print(name, g.quantile(0.75) - g.quantile(0.25))
Answer
For each age group the program prints the name of the group and the IQR of its years of coding, by the pandas method:
4, 6, 9 and 11.75 years. That is the width of each box in the boxplot.
A frequency table: age and the use of AI tools
Does the use of AI tools depend on age? The survey asked which AI tools the respondents use, with several options to choose from, one of which says that they do not use AI tools.
In the column ai_use the value is No for those who chose only that option, and Yes for those who chose at least one AI tool and not that option; the cell is empty for those who did not answer, and for 96 respondents who chose both.
Think: will the rate of those who do not use AI tools rise with age, fall, or change in some other way?
pd.crosstab counts only the rows that have a value in both columns; those who did not answer one of the questions are not in the table.
t = pd.crosstab(df['age_group'], df['ai_use'], margins=True)
print(t)
p = pd.crosstab(df['age_group'], df['ai_use'], normalize='index')
print(p.round(3))
ai_use No Yes All
age_group
18-24 years old 520 1738 2258
25-34 years old 714 4137 4851
35-44 years old 644 4378 5022
45-54 years old 366 2359 2725
55-64 years old 238 1062 1300
65 years or older 178 421 599
Prefer not to say 65 83 148
Under 18 years old 182 283 465
All 2907 14461 17368
ai_use No Yes
age_group
18-24 years old 0.230 0.770
25-34 years old 0.147 0.853
35-44 years old 0.128 0.872
45-54 years old 0.134 0.866
55-64 years old 0.183 0.817
65 years or older 0.297 0.703
Prefer not to say 0.439 0.561
Under 18 years old 0.391 0.609
Question 3. Describe in words how the conditional rate of “No” changes with age.
Are age and the use of AI tools roughly unrelated in these data?
What should we do with the row “Prefer not to say”?
Solution
The rate of “No” is high among the youngest (39.1% under 18, 23.0% at 18–24), falls to its lowest at 35–44 (12.8%), and rises again with age up to 29.7% at 65 or older.
Among the age groups the conditional rates range from 12.8% to 39.1%, so the variables are related:
this is not independence in the data. Nor does the relation simply “rise” or “fall”:
it is low in the middle and high at both ends.
The row “Prefer not to say” has the highest rate of all, 43.9%, but only 148 respondents, and it is not an age group, so it does not belong in a comparison by age.
We take it out of that comparison (or keep it apart, as here) and write down what we did.
The table covers 17,368 respondents, those who answered both the age question and the question on AI tools, out of 30,903.
What does this program compute? How does its result differ from the table of conditional rates above?
(!= means “is not equal to”.)
d = df[df['age_group'] != 'Prefer not to say']
q = pd.crosstab(d['age_group'], d['ai_use'], normalize='index')
print(q.round(3))
Answer
The program keeps only those who did not choose “Prefer not to say” in the age question, and prints the conditional rates by row.
The result is the same table without the row “Prefer not to say”, and the other numbers do not change:
each row is divided by its own row total, so removing one row does not affect the others.
With normalize=‘columns’ every number would change.
The inquiry tool: a point beyond the whisker
The tool “Same statistics, different picture” holds nine sets of points that we built ourselves, with the same means and the same standard deviations of and of .
The quartiles in the tool’s table are computed like quantile in pandas, and the boxplot uses the same quartiles.
In the interactive version of the tool we choose a set and see it as a scatter plot, a histogram, a boxplot, and a violin plot. The table summarises each set by the mean and the standard deviation of x and of y: the numbers are the same in every set, and the difference between the sets shows only in the picture.
| Set | Mean x | Mean y | SD x | SD y |
|---|---|---|---|---|
| Cloud | 52.40 | 47.60 | 19.60 | 17.80 |
| Star of David | 52.40 | 47.60 | 19.60 | 17.80 |
| Shofar | 52.40 | 47.60 | 19.60 | 17.80 |
| Dreidel | 52.40 | 47.60 | 19.60 | 17.80 |
| Sufganiya | 52.40 | 47.60 | 19.60 | 17.80 |
| Purim mask | 52.40 | 47.60 | 19.60 | 17.80 |
| Megillah | 52.40 | 47.60 | 19.60 | 17.80 |
| Hanukkah menorah | 52.40 | 47.60 | 19.60 | 17.80 |
| Map of Israel | 52.40 | 47.60 | 19.60 | 17.80 |
Question 4. Go through the sets in the tool with the boxplot of .
Only one of the nine sets shows a point beyond a whisker. Find it, check with the IQR rule and the quartiles in the tool’s table that the point really lies beyond the limit, and find in the scatter plot which part of the picture it belongs to.
Hint
The point is below the lower whisker. The lower limit is .
Solution
It is the map of Israel. In the tool’s table and for , so and the lower limit is .
The lowest point is at , below the limit, so it is drawn on its own.
In the scatter plot it is the southern tip of the map, near Eilat.
The point is a real part of the picture, not an error: the IQR rule flags values as “worth checking”, and such a check sometimes ends with the conclusion that the value is correct.
Question 5. Choose the Hanukkah menorah, the histogram and the variable .
Move the number of bins from 4 to 28. What appears only when the bins are narrow, and why?
Solution
With 4 bins there are four bars of different heights (36, 81, 51 and 32 points), with no sign of the candles.
With 28 bins there are nine tall bars, with 11 to 34 points each, while every other bar has at most 5:
the nine candles of the menorah, eight candles and the helper candle in the middle.
(With 40 bins two of the candles each fall on two neighbouring bars, so the count of tall bars is less clear.) Each candle is a narrow column of points with almost the same , and a wide bin counts several candles and the gaps between them together.
It is the same effect as in the histogram of years of coding:
wide bins smooth the shape, and narrow bins show concentrations at particular values.
Interpreting a graph: salary and the use of AI tools
The figure shows the annual salary of those who do not use AI tools and of those who do, on a logarithmic axis.
As in Same Statistics, Different Pictures, it keeps salaries from 1,000 to 1,000,000 dollars.
Question 6. The median of those who do not use AI tools is 73,923 dollars, and of those who do, 85,000 dollars.
Which conclusion does the figure support?
A: Using AI tools raises salary by about 15%
The reasoning: the median of the users is about 15% higher ().
The mistake: the figure compares different people; it does not check what happens to one person’s salary when they start using AI tools.
Users and non-users may also differ in other ways, such as country, role and age.
The survey describes a relation; it does not show what causes what.
B: The users' median is about 15% higher, and the distributions of the two groups overlap to a large extent
The reasoning: , and the boxes of the two groups overlap:
from 43,292 to 115,000 dollars for the non-users, and from 47,766 to 132,997 for the users.
C: Most AI tool users earn more than everyone who does not use them
The reasoning: the users’ violin is shifted a little to the right.
The mistake: the two shapes overlap almost entirely. The third quartile of the non-users, 115,000 dollars, is above the median of the users.
Many users have low salaries and many non-users have high ones.
D: On a logarithmic scale the medians of the two groups cannot be compared
The reasoning: the axis distorts distances, so the comparison is not fair.
The mistake: a logarithmic axis changes only the picture. The medians are the same numbers in dollars, and the order of the values does not change, so each group’s median sits at the same relative place.
On a logarithmic scale the distance between two medians shows the ratio between them.
Question 7. In the frequency table of age and the use of AI tools, 23.0% of those aged 18–24 do not use AI tools, against 12.8% of those aged 35–44, and salaries are lower in the younger age groups:
the median at 18–24 is 30,707 dollars, and at 35–44 95,308 dollars.
How can these facts create part of the gap in the figure? How would you check whether they explain the whole gap?
Solution
If the non-users include more young people, and young people earn less, the non-users’ median will be lower even if there is no gap within each age group.
In the data, 10.3% of the non-users who gave a salary are aged 18–24, against 7.8% of the users.
To check, we compare within each age group separately. In the data the gap remains in most groups:
at 35–44 the median is 88,908 dollars for the non-users and 96,669 for the users, and at 45–54 95,275 against 103,960.
At 18–24 it reverses: 32,277 against 30,000. So age does not explain the whole gap, but even within an age group the two groups may differ in country, role and experience.
Lengths of stay: what to tell patients, and how many beds are needed
The file lausanne-los-2000.csv holds the length of stay, in days, of 201 patients admitted to the University Hospital of Lausanne in 2000, in the column days (details of the source and licence).
The same data lead to two different decisions: what to tell a patient who is admitted, and how many beds the ward needs.
Think: a stay cannot be shorter than a day, but it can last months.
Which is larger in data like these, the mean or the median?
The program has two new commands: max() returns the largest value in the column, and tolist() turns the result of quantile into an ordinary Python list, so that it prints on one line.
import pandas as pd
los = pd.read_csv('lausanne-los-2000.csv')['days']
print(len(los))
print(los.median(), round(los.mean(), 2))
print(los.quantile([0.25, 0.75]).tolist())
print(los.max())
201
8.0 11.25
[4.0, 13.0]
102
Question 8.
- A patient admitted to the ward asks: “How long will I be here?”. What can we tell them from the data, and why not the mean?
- The ward expects 201 patients like these next year. How many beds will be occupied on an average day? Write a program that computes the total number of bed-days and divides it by 365, and compute also the result we would get if we used the median instead of the mean. Which of the two is right, and why?
- Should the ward plan for exactly the number from part 2?
Hint
The total number of bed-days is the sum of the days of all the patients:los.sum(). The sum equals the number of patients times the mean.
Solution
- The median is 8: half of the patients went home within 8 days, and about a quarter stayed more than 13 days (). We can say “usually about a week, sometimes two weeks or more”. The mean, 11.25, is longer than the stay of most patients, because a few long stays (up to 102 days) pull it up.
- The program:
bed_days = los.sum()
print(bed_days, round(bed_days / 365, 1))
by_median = len(los) * los.median()
print(by_median, round(by_median / 365, 1))
2261 6.2
1608.0 4.4
About 6.2 beds are occupied on an average day. The mean is the right one:
every day of a stay occupies a bed, including the days of the long stays, and among 201 patients a year there will probably again be a few long stays.
A computation by the median ignores them and gives 4.4 beds, almost two beds fewer than needed every day.
- No. The computation assumes that next year’s patients will resemble these, and 6.2 is an average over the year; on a given day more patients may be in the ward, for example when several long stays overlap. As with the margin before a flight in the practice by hand, the mean is enough for the total, but for the busy days we look at the high values.
What does this program compute? (> compares each value with the mean and gives True or False.)
print(round((los > los.mean()).mean(), 3))
Answer
The share of patients who stayed longer than the mean: 0.328, about a third.
The mean of a column of True and False is the share of True, because True counts as 1 and False as 0.
About two thirds of the patients stayed less than the mean, which is another way to see why the mean does not describe a typical patient.