Skip to content

Commit

Permalink
update CI backend (closes #29, #28)
Browse files Browse the repository at this point in the history
update CI backend to mpl ax.errorbar API (closes #29, ref #28)
  • Loading branch information
LSYS committed Oct 22, 2022
1 parent e12978a commit 1bf2c10
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
28 changes: 14 additions & 14 deletions forestplot/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ def draw_ci(
dataframe: pd.core.frame.DataFrame,
estimate: str,
yticklabel: str,
moerror: str,
ll: str,
hl: str,
ax: Axes,
**kwargs: Any
) -> Axes:
"""
Draw the confidence intervals using the horizontal bar plot (barh) from the pandas API.
Draw the confidence intervals using the Matplotlib errorbar API.
Parameters
----------
Expand All @@ -28,9 +29,10 @@ def draw_ci(
OR, regression estimates, etc.).
yticklabel (str)
Name of column in intermediate dataframe containing the formatted yticklabels.
moerror (str)
Name of column containing the margin of error in the confidence intervals.
Should be available if 'll' and 'hl' are left empty.
ll (str)
Name of column containing the lower limit of the confidence intervals.
hl (str)
Name of column containing the upper limit of the confidence intervals.
ax (Matplotlib Axes)
Axes to operate on.
Expand All @@ -40,15 +42,13 @@ def draw_ci(
"""
lw = kwargs.get("lw", 1.4)
linecolor = kwargs.get("linecolor", ".6")
ax = dataframe.plot(
y=estimate,
x=yticklabel,
kind="barh",
xerr=moerror,
color="none",
error_kw={"lw": lw, "ecolor": linecolor},
legend=False,
ax=ax,
ax.errorbar(
x=dataframe[estimate],
y=dataframe[yticklabel],
xerr=[dataframe[estimate] - dataframe[ll], dataframe[hl] - dataframe[estimate]],
ecolor=linecolor,
elinewidth=lw,
ls="none",
zorder=0,
)
return ax
Expand Down
5 changes: 2 additions & 3 deletions forestplot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ def forestplot(
Should be available if 'll' and 'hl' are left empty.
ll (str)
Name of column containing the lower limit of the confidence intervals.
Optional
hl (str)
Name of column containing the upper limit of the confidence intervals.
Optional
form_ci_report (bool)
If True, form the formatted confidence interval as a string.
ci_report (bool)
Expand Down Expand Up @@ -364,7 +362,8 @@ def _make_forestplot(
dataframe=dataframe,
estimate=estimate,
yticklabel=yticklabel,
moerror=moerror,
ll=ll,
hl=hl,
ax=ax,
**kwargs,
)
Expand Down
13 changes: 10 additions & 3 deletions tests/test_graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"yticklabel": str_vector,
"estimate": x,
"moerror": y,
"ll": x,
"hl": y,
"pval": y,
"formatted_pval": y,
"yticklabel1": str_vector,
Expand All @@ -38,12 +40,17 @@
def test_draw_ci():
_, ax = plt.subplots()
ax = draw_ci(
input_df, estimate="estimate", yticklabel="yticklabel", moerror="moerror", ax=ax
dataframe=input_df,
estimate="estimate",
yticklabel="yticklabel",
ll="ll",
hl="hl",
ax=ax,
)
assert isinstance(ax, Axes)

xticklabels = [lab.get_text() for lab in ax.get_yticklabels()]
assert all(txt in xticklabels for txt in input_df["yticklabel"])
# Assert yticklabels are of correct length
assert len(ax.get_yticklabels()) == len(input_df["yticklabel"])

# Assert yticks are integers
assert (all(isinstance(tick, int)) for tick in ax.get_yticks())
Expand Down

0 comments on commit 1bf2c10

Please sign in to comment.