Menu
CoddyTech

Histogram of a Column

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 50 of 60.

Are evening waits at a clinic longer than morning ones? Two histograms on one axes answer at a glance:

Two half see-through histograms of clinic waits: morning waits from 6 to 16 minutes on the left, evening waits from 13 to 28 minutes on the right, overlapping a little

A column goes straight into ax.hist. Keep each group's rows, then draw both on the same axes:

# waits has the columns shift and minutes
morning = waits[waits["shift"] == "morning"]
evening = waits[waits["shift"] == "evening"]
fig, ax = plt.subplots()
ax.hist(morning["minutes"], bins=4, alpha=0.5, label="morning")
ax.hist(evening["minutes"], bins=4, alpha=0.5, label="evening")
ax.set_title("Clinic Waits")
ax.set_xlabel("Minutes")
ax.legend()
plt.show()

alpha=0.5 makes bars half see-through (0 is invisible, 1 solid), so overlaps stay visible. label= names each for the legend.

When groups spread over different ranges, that column helps a model tell them apart.

challenge icon

Challenge

Medium

An office asked its staff how long the trip to work takes: commute.csv has mode (bike or bus) and minutes. Whose times are more spread out? The starter keeps each group in bike and bus and frames the chart.

Draw a histogram of each group's minutes with bins=5, alpha=0.5 and the label bike or bus.

Yours should look like this one, from last year's survey:

Two half see-through histograms titled Commute Times: bike times bunched between 14 and 26 minutes, bus times spread from 15 to 45 minutes, with a legend

The group whose bars cover more minutes is the more spread out. Coddy describes your chart in the output.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt

trips = pd.read_csv("commute.csv")
bike = trips[trips["mode"] == "bike"]
bus = trips[trips["mode"] == "bus"]

fig, ax = plt.subplots()
ax.set_title("Commute Times")
ax.set_xlabel("Minutes")

# Write your code below

# Don't change the lines below
ax.legend()
plt.show()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler