API reference#
Epidemic curves#
- epiplot.epicurve(data, date_col, *, date_type=None, interval='week', week_start='monday', group_col=None, count_col=None, incomplete_after=None, ax=None)#
Plot an epidemic curve: the number of cases in each time period.
- Parameters:
data (DataFrame) – A table with one row per case (a line list), or one row per count if
count_colis given.date_col (str) – Name of the column holding the dates.
date_type (Literal['onset', 'report', 'specimen', 'diagnosis', 'other'] | None) – What the dates mean:
"onset","report","specimen"or"diagnosis". This sets the x-axis label. Use"other"to get a plain label you can replace withax.set_xlabel(). If not given, anEpiplotWarningis shown and the axis says the date type was not specified.interval (Literal['day', 'week', 'month']) – Length of each bar:
"day","week"or"month".week_start (Literal['monday', 'sunday']) – First day of each week when
interval="week":"monday"for ISO weeks or"sunday"for US CDC (MMWR) weeks.group_col (str | None) – Optional column to split each bar by, such as travel history. Up to eight groups are shown in colour-blind-safe colours, plus a grey
"Missing"group for cases with no value.count_col (str | None) – Optional column holding a count for each row. If not given, each row counts as one case.
incomplete_after (str | Timestamp | None) – Optional date after which counts are likely to be too low because recent cases have not all been reported yet. That part of the plot is shaded and explained in the legend, starting from the whole period that contains this date. A common choice is the date the data was extracted minus the usual delay between the date in
date_coland a case being reported. epiplot does not work this date out from the data.ax (Axes | None) – Optional Matplotlib axes to draw on. If not given, a new figure is created.
- Returns:
The axes the epicurve was drawn on, for further customisation.
- Return type:
matplotlib.axes.Axes
- Raises:
ValueError – If an option is not recognised, or if there are more than eight groups (combine smaller groups into an “Other” group first).
See also
count_casesThe counts behind the plot, as a table.
- epiplot.count_cases(data, date_col, *, interval='week', week_start='monday', group_col=None, count_col=None)#
Count cases per time period, including periods with no cases.
- Parameters:
data (DataFrame) – A table with one row per case (a line list), or one row per count if
count_colis given.date_col (str) – Name of the column holding the dates. Dates stored as text are converted automatically.
interval (Literal['day', 'week', 'month']) – Length of each period:
"day","week"or"month".week_start (Literal['monday', 'sunday']) – First day of each week when
interval="week":"monday"for ISO weeks or"sunday"for US CDC (MMWR) weeks.group_col (str | None) – Optional column to count separately for each group, such as sex or travel history. Cases with a missing group are counted in a group called
"Missing", so no cases are silently dropped.count_col (str | None) – Optional column holding a count for each row. If not given, each row counts as one case.
- Returns:
One row per period (and per group, if
group_colis given), with the columnsperiod_start, the group column (if any) andcases. Every period from the first case to the last is included, with a count of zero where there were no cases. The number of cases left out because their date was missing is stored inresult.attrs["n_missing_dates"].- Return type:
pandas.DataFrame
- Raises:
KeyError – If a named column is not in
data.ValueError – If
intervalorweek_startis not a recognised option, or ifcount_colcontains missing, negative or non-whole numbers.
Rate maps#
- epiplot.rate_map(regions, data, region_col, *, cases_col=None, population_col=None, rate_col=None, rate_label=None, counted_over=None, per=100000, min_cases=5, bands=None, ax=None)#
Map rates by region, shading each region by its rate.
Give either
cases_colandpopulation_col, so crude rates are calculated, orrate_colandrate_labelfor rates you have already calculated, such as age-standardised rates.- Parameters:
regions (DataFrame) – A GeoPandas GeoDataFrame with one shape per region, and a
region_colcolumn identifying each region.data (DataFrame) – A table with one row per region.
region_col (str) – Name of the column identifying each region, in both
regionsanddata.cases_col (str | None) – Name of the column in
dataholding the number of cases. Also used withrate_col, to hide regions with small numbers of cases.population_col (str | None) – Name of the column in
dataholding the population.rate_col (str | None) – Name of a column in
dataholding rates already calculated.rate_label (str | None) – What the rates are, for the colour scale, for example
"Age-standardised rate per 100,000 people". Needed withrate_col.counted_over (str | None) – The length of time the cases were counted over, such as
"28 days"or"1 year". It is added to the colour scale label, for example “Crude rate per 100,000 people over 28 days”. This only labels the plot: epiplot can’t check it, so make sure it matches your data.per (float) – The rate is given per this many people, for example 100,000.
min_cases (int) – Regions with fewer cases than this are hidden, because rates from very small numbers are unreliable and may identify individuals.
bands (int | Sequence[float] | None) – Leave out for a smooth colour scale starting at zero. Give a number, such as 5, to group rates into about that many colour bands, or a list of band boundaries, such as
[0, 10, 20, 50].ax (Axes | None) – Optional Matplotlib axes to draw on. If not given, a new figure is created.
- Returns:
The axes the map was drawn on, for further customisation.
- Return type:
matplotlib.axes.Axes
- Raises:
ImportError – If GeoPandas is not installed. Install it with
pip install "epiplot[maps]".ValueError – If neither
cases_colandpopulation_colnorrate_colis given, or ifrate_colis given withoutrate_label.
See also
calculate_ratesThe crude rates behind the map, as a table.
rate_map_timelineOne small map per period, sharing one colour scale.
- epiplot.rate_map_timeline(regions, data, region_col, period_col, *, cases_col=None, population_col=None, rate_col=None, rate_label=None, counted_over=None, per=100000, min_cases=5, bands=None, ncols=None)#
Draw one small rate map per period, all sharing one colour scale.
Every map uses the same colours for the same rates, so maps from different periods can be compared fairly. The options are the same as for
rate_map(), plusperiod_colandncols.- Parameters:
regions (DataFrame) – A GeoPandas GeoDataFrame with one shape per region, and a
region_colcolumn identifying each region.data (DataFrame) – A table with one row per region and period.
region_col (str) – Name of the column identifying each region, in both
regionsanddata.period_col (str) – Name of the column in
dataholding the period, such as a year or the start of a week. Maps are shown in order of this column. Dates are labelled like “02 Mar 2026”; to label them differently, convert the column to text or an ordered categorical first.cases_col (str | None) – As for
rate_map().population_col (str | None) – As for
rate_map().rate_col (str | None) – As for
rate_map().rate_label (str | None) – As for
rate_map().counted_over (str | None) – As for
rate_map().per (float) – As for
rate_map().min_cases (int) – As for
rate_map().bands (int | Sequence[float] | None) – As for
rate_map().ncols (int | None) – Number of maps in each row. If not given, up to 4.
- Returns:
The figure holding all the maps. Each map is in
figure.axes.- Return type:
matplotlib.figure.Figure
- Raises:
ImportError – If GeoPandas is not installed. Install it with
pip install "epiplot[maps]".ValueError – If there are more than 24 periods (combine them into longer periods first), or for the same reasons as
rate_map().
See also
rate_mapA single map.
calculate_ratesThe crude rates behind the maps, as a table.
- epiplot.calculate_rates(data, region_col, cases_col, population_col, *, per=100000, min_cases=5, period_col=None, confidence=0.95)#
Calculate crude rates per population, with exact confidence intervals.
- Parameters:
data (DataFrame) – A table with one row per region, or one row per region and period if
period_colis given.region_col (str) – Name of the column identifying each region, such as an area code.
cases_col (str) – Name of the column holding the number of cases.
population_col (str) – Name of the column holding the population.
per (float) – The rate is given per this many people, for example 100,000.
min_cases (int) – Regions with fewer cases than this are marked
"hidden", because rates from very small numbers are unreliable and may identify individuals.period_col (str | None) – Optional column holding a time period, such as a week or year, for rates over time.
confidence (float) – Confidence level for the interval, for example 0.95 for 95%.
- Returns:
One row per region (and period), with the columns
region_col,period_col(if given),cases,population,rate,rate_lower,rate_upperandstatus. The confidence interval uses the exact Poisson method, which is accurate even for small counts.statusis"shown","hidden"(fewer thanmin_casescases) or"no data"(cases or population missing, or a population of zero). Rates are still calculated for hidden regions, so remove them before publishing the table.- Return type:
pandas.DataFrame
- Raises:
KeyError – If a named column is not in
data.ValueError – If a region appears more than once (per period), if cases are not whole numbers of zero or more, if a population is negative, or if
per,min_casesorconfidenceis out of range.
Survival curves#
- epiplot.survival_curve(data, time_col, event_col, *, time_unit=None, group_col=None, confidence=0.95, at_risk_times=None, ax=None)#
Plot Kaplan-Meier survival curves, with a number-at-risk table.
Each curve shows the share of people still free of the event over time, with a shaded confidence band. Small tick marks show when people were censored. The table under the plot shows how many people were still being followed at each labelled time, because the right-hand end of a curve often rests on very few people.
- Parameters:
data (DataFrame) – A table with one row per person.
time_col (str) – Name of the column holding each person’s follow-up time.
event_col (str) – Name of the column saying whether the event happened (1 or True) or the person was censored (0 or False).
time_unit (Literal['days', 'weeks', 'months', 'years', 'other'] | None) – What the times are measured in:
"days","weeks","months"or"years". This sets the x-axis label. Use"other"to get a plain label you can replace withax.set_xlabel(). If not given, anEpiplotWarningis shown and the axis says the unit was not specified.group_col (str | None) – Optional column to draw a separate curve for each group, such as treatment. Up to eight groups are shown in colour-blind-safe colours, plus a grey
"Missing"group for people with no value.confidence (float) – Confidence level for the shaded band, for example 0.95 for 95%.
at_risk_times (Sequence[float] | None) – Optional times at which to show the number at risk. If not given, the labelled times on the x axis are used.
ax (Axes | None) – Optional Matplotlib axes to draw on. If not given, a new figure is created.
- Returns:
The axes the curves were drawn on, for further customisation.
- Return type:
matplotlib.axes.Axes
- Raises:
ValueError – If an option is not recognised, or if there are more than eight groups.
See also
kaplan_meierThe estimates behind the plot, as a table.
- epiplot.kaplan_meier(data, time_col, event_col, *, group_col=None, confidence=0.95)#
Estimate the share of people still free of the event over time.
Uses the Kaplan-Meier method, which allows for people who leave the study early or have not had the event by the end (“censored” people).
- Parameters:
data (DataFrame) – A table with one row per person.
time_col (str) – Name of the column holding each person’s follow-up time, such as days from diagnosis to the event or to the end of follow-up.
event_col (str) – Name of the column saying whether the event happened (1 or True) or the person was censored (0 or False).
group_col (str | None) – Optional column to estimate a separate curve for each group, such as treatment. Missing values are grouped as
"Missing".confidence (float) – Confidence level for the interval, for example 0.95 for 95%.
- Returns:
One row per time at which something happened, plus a first row at time 0, with the columns
group_col(if given),time,at_risk,events,censored,survival,survival_lowerandsurvival_upper.at_riskcounts the people still being followed just before that time. People censored at the same time as an event are counted as at risk of it. The confidence interval uses the log-log method, which keeps it between 0 and 1. It is missing once survival reaches 0. The number of rows left out because the time or event was missing is inresult.attrs["n_missing"].- Return type:
pandas.DataFrame
- Raises:
KeyError – If a named column is not in
data.ValueError – If a time is negative, if an event is not 0, 1, True or False, or if
confidenceis out of range.
Warnings#
- class epiplot.EpiplotWarning#
A nudge about good practice, such as an unlabelled date type.
To hide only epiplot’s warnings, and keep warnings from other libraries:
import warnings from epiplot import EpiplotWarning warnings.simplefilter("ignore", EpiplotWarning)