def MakeHists(live): """ Plot Hists for live births :param live: DataFrame :return: DataFrame """ hst = thinkstats2.Hist(live.birthwgt_lb, label='birthwgt_lb') thinkplot.Hist(hist) # thinkplot.Show() thinkplot.Save(root='../figure/first_wgt_lb_hist', xlabel='pounds', ylabel='frequency', axis=[-1, 14, 0, 3200]) hist = thinkstats2.Hist(live.birthwgt_oz, label='birthwgt_oz') thinkplot.Hist(hist) thinkplot.Save(root='../figure/first_wgt_oz_hist', xlabel='pounds', ylabel='frequency', axis=[-1, 16, 0, 1200]) hist = thinkstats2.Hist(np.floor(live.agepreg), label='agepreg') thinkplot.Hist(hist) thinkplot.Save(root='../figure/first_agepreg_hist', xlabel='weeks', ylabel='frequency') hist = thinkstats2.Hist(live.prglngth, label='prglngth') thinkplot.Hist(hist) thinkplot.Save(root='../figure/first_prglngth_hist', xlabel='weeks', ylabel='frequency', axis=[-1, 53, 0, 5000])
def MakeComparison(firsts, others): """ Plots histograms of pregenancy length for first babies and others. :param firsts: DataFrame :param others: DataFrame """ first_hist = thinkstats2.Hist(firsts.prglngth, label='first') other_hist = thinkstats2.Hist(others.prglngth, label='other') width = 0.45 thinkplot.PrePlot(2) thinkplot.Hist(first_hist, align='right', width=width) thinkplot.Hist(other_hist, align='left', width=width) thinkplot.Save(root="../figure/fisrt_nsfg_hist", title='Histogram', xlabel='weeks', ylabel='frequency', axis=[27, 46, 0, 2700])
def MakeFigures(firsts, others): """ Plot Pmfs of pregnancy length. :param firsts: DataFrame :param ohters: DataFrame """ # plot th PMFs first_pmf = thinkstats2.Pmf(firsts.prglngth, label='first') other_pmf = thinkstats2.Pmf(others.prglngth, label='other') width = 0.45 thinkplot.PrePlot(2, cols=2) thinkplot.Hist(first_pmf, align='right', width=width) thinkplot.Hist(other_pmf, align='left', width=width) thinkplot.Config(xlabel='weeks', ylabel='probability', axis=[27, 46, 0, 0.6]) thinkplot.PrePlot(2) thinkplot.SubPlot(2) thinkplot.Pmfs([first_pmf, other_pmf]) thinkplot.Save(root='../figure/probability_nsfg_pmf', xlabe='weeks', axis=[27, 46, 0, 0.6]) # plot the differences in the PMFs weeks = range(35, 46) diffs = [] for week in weeks: p1 = first_pmf.Prob(week) p2 = other_pmf.Prob(week) diff = 100 * (p1 - p2) diffs.append(diff) thinkplot.Bar(weeks, diffs) thinkplot.Save(root='../figure/probability_nsfg_diffs', title='Difference in PMFs', xlabel='week', ylabel='percentage points', legend=False)
def MakeHists(live): """ Plot Hists for live births. :param live: DataFrame :return: DataFrame """ hist = thinkstats2.Hist(np.floor(live.agepreg), label='agepreg') thinkplot.PrePlot(2, cols=2) thinkplot.SubPlot(1) thinkplot.Hist(hist) thinkplot.Config(xlabel='years', ylabel='frequency', axis=[0, 45, 0, 700]) thinkplot.SubPlot(2) thinkplot.Pmf(hist) thinkplot.Save(root='../figure/probability_agepreg_hist', xlabel='years', axis=[0, 45, 0, 700])
def PrintExtremes(live): """ Plots the histogram of pregnancy lengths and prints the extremes. :param live: DataFrame of live births """ hist = thinkstats2.Hist(live.prglngth) thinkplot.Hist(hist, label='live births') thinkplot.Save(root='../figure/first_nsfg_hist_live', title='Histogram', xlabel='weeks', ylabel='frequency', axis=[-1, 51, 0, 5000]) print('Shortest lengths: ') for weeks, freq in hist.Smallest(10): print(weeks, freq) print('Longest length:') for weeks, freq in hist.Largest(10): print(weeks, freq)
# coding: utf-8 """ Chapter 2 """ import src.thinkplot as thinkplot import src.thinkstats2 as thinkstats2 hist = thinkstats2.Hist([1, 2, 2, 3, 5]) thinkplot.Hist(hist) thinkplot.Show(xlabel='value', ylabel='frequency', align='right')