Skip to content

Commit

Permalink
Add docstring & test for (#88, #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
LSYS committed Dec 16, 2023
1 parent 9fd352f commit ab93c4c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
29 changes: 24 additions & 5 deletions forestplot/mplot_graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,37 @@ def mdraw_ref_xline(
return ax


# =============================================================================================
# =============================================================================================
# =============================================================================================
def mdraw_yticklabels(
dataframe: pd.core.frame.DataFrame,
yticklabel: str,
model_col: str,
models: Optional[Union[Sequence[str], None]],
flush: bool,
ax: Axes,
**kwargs: Any,
) -> Axes:
"""
Set custom y-axis tick labels on a matplotlib Axes object using the yticklabel column in the provided
pandas dataframe.
Parameters
----------
dataframe : pd.core.frame.DataFrame
The pandas DataFrame from which the y-axis tick labels are derived.
yticklabel : str
Column name in the DataFrame whose values are used as y-axis tick labels.
flush : bool
If True, aligns y-axis tick labels to the left with adjusted padding to prevent overlap.
If False, aligns labels to the right.
ax : Axes
The matplotlib Axes object to be modified.
**kwargs : Any
Additional keyword arguments for customizing the appearance of the tick labels.
Supported customizations include 'fontfamily' (default 'monospace') and 'fontsize' (default 12).
Returns
-------
Axes
The modified matplotlib Axes object with updated y-axis tick labels.
"""
ax.set_yticks(range(len(dataframe)))

fontfamily = kwargs.get("fontfamily", "monospace")
Expand Down
46 changes: 46 additions & 0 deletions tests/test_mplot_graph_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from forestplot.mplot_graph_utils import mdraw_ref_xline, mdraw_yticklabels
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.pyplot import Axes


x, y = [0, 1, 2], [0, 1, 2]
str_vector = ["a", "b", "c"]
input_df = pd.DataFrame(
{
"yticklabel": str_vector,
"estimate": x,
"moerror": y,
"ll": x,
"hl": y,
"pval": y,
"formatted_pval": y,
"yticklabel1": str_vector,
"yticklabel2": str_vector,
}
)


def test_mdraw_ref_xline():
_, ax = plt.subplots()
ax = mdraw_ref_xline(ax, dataframe=input_df, model_col="yticklabel", annoteheaders=None, right_annoteheaders=None)
assert isinstance(ax, Axes)


def test_mdraw_yticklabels():
# Prepare the input DataFrame
x = [0, 1, 2]
str_vector = ["a", "b", "c"]
input_df = pd.DataFrame({
"yticklabel": str_vector,
})

# Create a matplotlib Axes object
_, ax = plt.subplots()

# Call the function
ax = mdraw_yticklabels(input_df, yticklabel='yticklabel',flush=True, ax=ax)

assert isinstance(ax, Axes)
assert [label.get_text() for label in ax.get_yticklabels()] == str_vector

0 comments on commit ab93c4c

Please sign in to comment.