Quantitative comparisons: histograms

Introduction to Data Visualization with Matplotlib

Ariel Rokem

Data Scientist

Histograms

Introduction to Data Visualization with Matplotlib

A bar chart again

fig, ax = plt.subplots()

ax.bar("Rowing", mens_rowing["Height"].mean())
ax.bar("Gymnastics", mens_gymnastics["Height"].mean())
ax.set_ylabel("Height (cm)") plt.show()

Introduction to Data Visualization with Matplotlib

Introducing histograms

fig, ax = plt.subplots()

ax.hist(mens_rowing["Height"])
ax.hist(mens_gymnastics["Height"])
ax.set_xlabel("Height (cm)") ax.set_ylabel("# of observations") plt.show()

Introduction to Data Visualization with Matplotlib

Labels are needed

ax.hist(mens_rowing["Height"], label="Rowing")
ax.hist(mens_gymnastics["Height"], label="Gymnastics")
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")

ax.legend() plt.show()

Introduction to Data Visualization with Matplotlib

Customizing histograms: setting the number of bins

ax.hist(mens_rowing["Height"], label="Rowing", bins=5)
ax.hist(mens_gymnastics["Height"], label="Gymnastics", bins=5)
ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()

Introduction to Data Visualization with Matplotlib

Customizing histograms: setting bin boundaries

ax.hist(mens_rowing["Height"], label="Rowing",
        bins=[150, 160, 170, 180, 190, 200, 210])

ax.hist(mens_gymnastics["Height"], label="Gymnastics",
        bins=[150, 160, 170, 180, 190, 200, 210])

ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()

Introduction to Data Visualization with Matplotlib

Customizing histograms: transparency

ax.hist(mens_rowing["Height"], label="Rowing",
        bins=[150, 160, 170, 180, 190, 200, 210],
        histtype="step")

ax.hist(mens_gymnastics["Height"], label="Gymnastics",
        bins=[150, 160, 170, 180, 190, 200, 210],
        histtype="step")

ax.set_xlabel("Height (cm)")
ax.set_ylabel("# of observations")
ax.legend()
plt.show()
Introduction to Data Visualization with Matplotlib

Histogram with a histtype of step

Introduction to Data Visualization with Matplotlib

Create your own histogram!

Introduction to Data Visualization with Matplotlib

Preparing Video For Download...