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:
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
MediumAn 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:
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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
7pandas DataFrames
SeriesDataFramesReading CSV FilesInspecting DataSelecting ColumnsRecap: Movie Ratings10Plotting DataFrames
Plotting a DataFrameScatter from ColumnsOne Color per GroupHistogram of a ColumnRecap: Cafe Sales2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler