Skip to content

Commit

Permalink
Merge pull request #115 from opensafely/iaindillingham/improve-graph-…
Browse files Browse the repository at this point in the history
…titles

Improve graph titles
  • Loading branch information
iaindillingham authored Dec 13, 2023
2 parents 367006e + 423f13e commit b0ac1e1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions analysis/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import click
import pandas
from matplotlib import pyplot
from matplotlib.dates import num2date

from analysis import click_types, utils

Expand Down Expand Up @@ -41,7 +40,7 @@ def main(from_date, from_offset, d_out):

utils.makedirs(d_out)

figs_cols = plot(by_day, by_week)
figs_cols = plot(by_day, by_week, get_plot_title(from_date, from_offset))
for fig, col in figs_cols:
f_stem = utils.slugify(col)
fig.savefig(d_out / f"{f_stem}.png")
Expand Down Expand Up @@ -77,7 +76,14 @@ def get_date_ranges_from_offset(data_frame, from_offset):
yield _DateRange(table_name, from_, to_)


def plot(by_day, by_week):
def get_plot_title(from_date, from_offset):
if from_date is not None:
return f"Event activity for the period from {utils.date_format(from_date)} to the report run date"
if from_offset is not None:
return f"The last {from_offset} days of event activity"


def plot(by_day, by_week, plot_title):
cols = by_day.columns.union(by_week.columns)
for col in cols:
fig, ax = pyplot.subplots(figsize=(15, 7))
Expand All @@ -99,8 +105,7 @@ def plot(by_day, by_week):

ax.grid(True)
ax.margins(x=0)
min_ts, max_ts = [num2date(x) for x in ax.get_xlim()]
ax.set_title(f"From {min_ts:%Y-%m-%d} to {max_ts:%Y-%m-%d}", fontsize="medium")
ax.set_title(plot_title)
ax.set_ylabel("Event Counts")
ax.set_ylim(0)
ax.legend(loc="upper right")
Expand Down
7 changes: 1 addition & 6 deletions analysis/render_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ def b64encode(path):
return f"data:{mtype};base64, {encoded}"


def date_format(date):
"""Formats the given date as, for example, "1 January 2023"."""
return f"{date:%-d %B %Y}" # the - removes the leading zero, but not on Windows


# register template filters
ENVIRONMENT.filters["b64encode"] = b64encode
ENVIRONMENT.filters["date_format"] = date_format
ENVIRONMENT.filters["date_format"] = utils.date_format


def render_report(data):
Expand Down
5 changes: 5 additions & 0 deletions analysis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ def slugify(s):
# remove leading and trailing dashes and underscores
s = s.strip("-_")
return s.lower()


def date_format(date):
"""Formats the given date as, for example, "1 January 2023"."""
return f"{date:%-d %B %Y}" # the - removes the leading zero, but not on Windows

0 comments on commit b0ac1e1

Please sign in to comment.