T2 - Plotting with FPsim

Basic plotting with FPsim is pretty straightforward.

If you’re new to FPsim, please start with tutorial T1 in order to install FPsim and learn how to run a basic simulation.

The basic design philosophy of FPsim is: common tasks should be simple. For example:

  • Defining parameters

  • Running a simulation

  • Plotting results

In tutorial T1 we learned how to define parameters and run a simulation. In this tutorial, we will take on our third simple task and plot the results of a simulation.

In tutorial T1, we showed you that to create, run, and plot a sim with default options is just:

[1]:
import fpsim as fp

sim = fp.Sim()
sim.run()
fig = sim.plot()
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-fpsim/envs/v0.19.2/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
  Running 1960 of 2020 ( 0/721) (0.09 s)  ———————————————————— 0%
  Running 1961 of 2020 (12/721) (0.21 s)  ———————————————————— 2%
  Running 1962 of 2020 (24/721) (0.35 s)  ———————————————————— 3%
  Running 1963 of 2020 (36/721) (0.48 s)  •——————————————————— 5%
  Running 1964 of 2020 (48/721) (0.61 s)  •——————————————————— 7%
  Running 1965 of 2020 (60/721) (0.74 s)  •——————————————————— 8%
  Running 1966 of 2020 (72/721) (0.88 s)  ••—————————————————— 10%
  Running 1967 of 2020 (84/721) (1.01 s)  ••—————————————————— 12%
  Running 1968 of 2020 (96/721) (1.14 s)  ••—————————————————— 13%
  Running 1969 of 2020 (108/721) (1.27 s)  •••————————————————— 15%
  Running 1970 of 2020 (120/721) (1.41 s)  •••————————————————— 17%
  Running 1971 of 2020 (132/721) (1.54 s)  •••————————————————— 18%
  Running 1972 of 2020 (144/721) (1.68 s)  ••••———————————————— 20%
  Running 1973 of 2020 (156/721) (1.81 s)  ••••———————————————— 22%
  Running 1974 of 2020 (168/721) (1.95 s)  ••••———————————————— 23%
  Running 1975 of 2020 (180/721) (2.08 s)  •••••——————————————— 25%
  Running 1976 of 2020 (192/721) (2.22 s)  •••••——————————————— 27%
  Running 1977 of 2020 (204/721) (2.35 s)  •••••——————————————— 28%
  Running 1978 of 2020 (216/721) (2.49 s)  ••••••—————————————— 30%
  Running 1979 of 2020 (228/721) (2.63 s)  ••••••—————————————— 32%
  Running 1980 of 2020 (240/721) (2.76 s)  ••••••—————————————— 33%
  Running 1981 of 2020 (252/721) (2.90 s)  •••••••————————————— 35%
  Running 1982 of 2020 (264/721) (3.04 s)  •••••••————————————— 37%
  Running 1983 of 2020 (276/721) (3.18 s)  •••••••————————————— 38%
  Running 1984 of 2020 (288/721) (3.31 s)  ••••••••———————————— 40%
  Running 1985 of 2020 (300/721) (3.46 s)  ••••••••———————————— 42%
  Running 1986 of 2020 (312/721) (3.59 s)  ••••••••———————————— 43%
  Running 1987 of 2020 (324/721) (3.73 s)  •••••••••——————————— 45%
  Running 1988 of 2020 (336/721) (3.88 s)  •••••••••——————————— 47%
  Running 1989 of 2020 (348/721) (4.02 s)  •••••••••——————————— 48%
  Running 1990 of 2020 (360/721) (4.16 s)  ••••••••••—————————— 50%
  Running 1991 of 2020 (372/721) (4.30 s)  ••••••••••—————————— 52%
  Running 1992 of 2020 (384/721) (4.45 s)  ••••••••••—————————— 53%
  Running 1993 of 2020 (396/721) (4.59 s)  •••••••••••————————— 55%
  Running 1994 of 2020 (408/721) (4.73 s)  •••••••••••————————— 57%
  Running 1995 of 2020 (420/721) (4.88 s)  •••••••••••————————— 58%
  Running 1996 of 2020 (432/721) (5.02 s)  ••••••••••••———————— 60%
  Running 1997 of 2020 (444/721) (5.18 s)  ••••••••••••———————— 62%
  Running 1998 of 2020 (456/721) (5.33 s)  ••••••••••••———————— 63%
  Running 1999 of 2020 (468/721) (5.48 s)  •••••••••••••——————— 65%
  Running 2000 of 2020 (480/721) (5.63 s)  •••••••••••••——————— 67%
  Running 2001 of 2020 (492/721) (5.78 s)  •••••••••••••——————— 68%
  Running 2002 of 2020 (504/721) (5.92 s)  ••••••••••••••—————— 70%
  Running 2003 of 2020 (516/721) (6.07 s)  ••••••••••••••—————— 72%
  Running 2004 of 2020 (528/721) (6.35 s)  ••••••••••••••—————— 73%
  Running 2005 of 2020 (540/721) (6.50 s)  •••••••••••••••————— 75%
  Running 2006 of 2020 (552/721) (6.65 s)  •••••••••••••••————— 77%
  Running 2007 of 2020 (564/721) (6.81 s)  •••••••••••••••————— 78%
  Running 2008 of 2020 (576/721) (6.96 s)  ••••••••••••••••———— 80%
  Running 2009 of 2020 (588/721) (7.12 s)  ••••••••••••••••———— 82%
  Running 2010 of 2020 (600/721) (7.27 s)  ••••••••••••••••———— 83%
  Running 2011 of 2020 (612/721) (7.43 s)  •••••••••••••••••——— 85%
  Running 2012 of 2020 (624/721) (7.59 s)  •••••••••••••••••——— 87%
  Running 2013 of 2020 (636/721) (7.74 s)  •••••••••••••••••——— 88%
  Running 2014 of 2020 (648/721) (7.91 s)  ••••••••••••••••••—— 90%
  Running 2015 of 2020 (660/721) (8.07 s)  ••••••••••••••••••—— 92%
  Running 2016 of 2020 (672/721) (8.23 s)  ••••••••••••••••••—— 93%
  Running 2017 of 2020 (684/721) (8.39 s)  •••••••••••••••••••— 95%
  Running 2018 of 2020 (696/721) (8.55 s)  •••••••••••••••••••— 97%
  Running 2019 of 2020 (708/721) (8.72 s)  •••••••••••••••••••— 98%
  Running 2020 of 2020 (720/721) (8.88 s)  •••••••••••••••••••• 100%

Final population size: 6139.
Run finished for "None" after 8.9 s
../_images/tutorials_T2_plotting_2_2.svg

Let’s take a look at the basic suite of plotting options, once we’ve run our initial simulation. The basic plot function will plot births, deaths, and mcpr over the entire simulation. There are also pre-defined options that combine similar types of output. For instance, ‘cpr’ will provide all definitions we use for contraceptive prevalence rates - mCPR, CPR, and aCPR. ‘apo’ stands for adverse pregnancy outcomes, and will plot infant deaths, stillbirths, and abortions.

[2]:
sim.plot()
sim.plot('cpr')
sim.plot('apo')
../_images/tutorials_T2_plotting_4_0.svg
../_images/tutorials_T2_plotting_4_1.svg
../_images/tutorials_T2_plotting_4_2.svg
[2]:
../_images/tutorials_T2_plotting_4_3.svg

We can also plot our key output for specific (pre-defined) age groups, since we know that family planning and building changes over womens’ lives. Here, we are plotting age-specific contraceptive prevalence.

[3]:
# CK: not sure if we want to enable this
# sim.plot('as_cpr')

This will generate a results dictionary sim.results. For example, the number of pregnancies in the sim can be found using sim.results['pregnancies'].

Rather than creating a parameter dictionary, any valid parameter can also be passed to the sim directly. For example, exactly equivalent to the above is: