Login
📚 Python for Introductory Statistics: Lab Workbook
Chapters ▾

5.4 Histogram, Polygon and Ogive

Watch demo video

Lab 5.4.1

A random sample of 54 annual salaries of Aviation employees of the City of Chicago is listed below. Write a Python code to construct a histogram with 6 classes(bins).

46776, 53736, 53736, 54840, 57408, 57408, 57408, 57408, 57408, 57408, 60108, 60108, 62712, 63552, 66852, 66852, 69732, 70032, 70044, 70272, 72024, 73380, 73380, 73380, 75408, 75468, 76248, 80484, 80484, 80484, 82476, 82788, 82836, 83676, 84324, 84324, 84324, 85848, 87564, 88272, 88272, 90828, 92004, 92304, 92520, 96060, 96096, 100680, 100716, 100716, 103764, 105420, 109620, 146868

view answer

Lab 5.4.2

Example: A random sample of 54 annual salaries of Aviation employees of the City of Chicago is listed below. Write a Python code using matplotlib to construct a histogram with 6 classes(bins).

46776, 53736, 53736, 54840, 57408, 57408, 57408, 57408, 57408, 57408, 60108, 60108, 62712, 63552, 66852, 66852, 69732, 70032, 70044, 70272, 72024, 73380, 73380, 73380, 75408, 75468, 76248, 80484, 80484, 80484, 82476, 82788, 82836, 83676, 84324, 84324, 84324, 85848, 87564, 88272, 88272, 90828, 92004, 92304, 92520, 96060, 96096, 100680, 100716, 100716, 103764, 105420, 109620, 146868

view answer

Polygon

Watch demo video

def polygon(x,n):
  import matplotlib.pyplot as plt
  import numpy as np

  freq, bins, edges = plt.hist(x,bins=n,ec="k",range=[min(x),max(x)+1])
  plt.title("Polygon")
  plt.xlabel('data values')
  plt.ylabel('frequency')

  a1=bins[0]-(bins[1]-bins[0])
  a2=bins[-1]+(bins[1]-bins[0])
  bin1=np.insert(bins,[0],a1)
  bin1=np.append(bin1,a2)

  freq,edges,_=plt.hist(x,bins=bin1,ec='k', range=[min(x),max(x)+1])
  midpoints=0.5*(edges[1:]+edges[:-1])
  plt.plot(midpoints,freq)
  plt.xticks(bin1)
  plt.show()

Lab 5.4.3

A random sample of 54 annual salaries of Aviation employees of the City of Chicago is listed below. Use the polygon function to construct a polygon with 6 classes(bins).

46776, 53736, 53736, 54840, 57408, 57408, 57408, 57408, 57408, 57408, 60108, 60108, 62712, 63552, 66852, 66852, 69732, 70032, 70044, 70272, 72024, 73380, 73380, 73380, 75408, 75468, 76248, 80484, 80484, 80484, 82476, 82788, 82836, 83676, 84324, 84324, 84324, 85848, 87564, 88272, 88272, 90828, 92004, 92304, 92520, 96060, 96096, 100680, 100716, 100716, 103764, 105420, 109620, 146868

view answer

Ogive

Watch demo video

def ogive(x,n):
  import matplotlib.pyplot as plt
  import numpy as np

  freq, bins, edges = plt.hist(x,bins=n,ec="k",range=[min(x),max(x)+1])
  plt.title("Ogive")
  plt.xlabel('data values')
  plt.ylabel('Cumulative frequency')

  a1=bins[0]-(bins[1]-bins[0])
  a2=bins[-1]+(bins[1]-bins[0])
  bin1=np.insert(bins,[0],a1)
  bin1=np.append(bin1,a2)

  freq,edges,_=plt.hist(x,bins=bin1,ec='k', range=[min(x),max(x)+1])

  ogive1 = edges[:-1]
  cumfreq = np.cumsum(freq)
  plt.plot(ogive1,cumfreq)
  plt.xticks(bin1)
  plt.show()

Lab 5.4.4

A random sample of 54 annual salaries of Aviation employees of the City of Chicago is listed below. Use the ogive function to construct an ogive with 6 classes(bins).

46776, 53736, 53736, 54840, 57408, 57408, 57408, 57408, 57408, 57408, 60108, 60108, 62712, 63552, 66852, 66852, 69732, 70032, 70044, 70272, 72024, 73380, 73380, 73380, 75408, 75468, 76248, 80484, 80484, 80484, 82476, 82788, 82836, 83676, 84324, 84324, 84324, 85848, 87564, 88272, 88272, 90828, 92004, 92304, 92520, 96060, 96096, 100680, 100716, 100716, 103764, 105420, 109620, 146868

view answer

Adapted from Python for Introductory Statistics: Student's Lab Workbook, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.