Skip to content

Commit

Permalink
Add docstring & test for mdraw_yticklabel2 (#88, #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
LSYS committed Dec 16, 2023
1 parent 6a60ff8 commit ffa043e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
28 changes: 28 additions & 0 deletions forestplot/mplot_graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,34 @@ def mdraw_yticklabel2(
ax: Axes,
**kwargs: Any,
) -> Axes:
"""
Add custom y-axis labels (rightannote) to the right side of a matplotlib Axes object based on a DataFrame.
Parameters
----------
dataframe : pd.core.frame.DataFrame
The pandas DataFrame containing the data. The 'yticklabel2' column is used for the y-axis labels.
annoteheaders : Union[Sequence[str], None]
A sequence of strings specifying additional annotation headers to be displayed. If None, no additional headers are used.
right_annoteheaders : Union[Sequence[str], None]
A sequence of strings specifying additional right-aligned annotation headers to be displayed. If None, no additional headers are used.
ax : Axes
The matplotlib Axes object on which to add the labels.
**kwargs : Any
Additional keyword arguments for customization. Supported customizations include 'grouplab_fontweight'
(font weight for the group label, default 'bold'), 'fontfamily' (font family, default 'monospace'),
and 'fontsize' (font size, default 12).
Returns
-------
Tuple
A tuple containing the modified Axes object and the width of the rightmost text label.
Notes
-----
- The function adjusts the position of each label based on the presence of annotation headers.
- The returned 'righttext_width' can be used for further layout adjustments, especially in complex plots.
"""
grouplab_fontweight = kwargs.get("grouplab_fontweight", "bold")
fontfamily = kwargs.get("fontfamily", "monospace")
fontsize = kwargs.get("fontsize", 12)
Expand Down
28 changes: 26 additions & 2 deletions tests/test_mplot_graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mdraw_est_markers,
mdraw_legend,
mdraw_ref_xline,
mdraw_yticklabels,
mdraw_yticklabels, mdraw_yticklabel2
)

x, y = [0, 1, 2], [0, 1, 2]
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_mdraw_ci():

def test_mdraw_legend():
# Create a simple plot
fig, ax = plt.subplots()
_, ax = plt.subplots()
ax.plot([0, 1], [0, 1], marker="o", color="0")
ax.plot([0, 1], [1, 0], marker="s", color="0.4")

Expand All @@ -128,3 +128,27 @@ def test_mdraw_legend():
for line, color in zip(legend.legendHandles, mcolor):
assert isinstance(line, Line2D), "Legend entry is not a Line2D instance."
assert line.get_color() == color, "Legend marker color does not match."



def test_mdraw_yticklabel2():
# Create sample DataFrame
df = pd.DataFrame({
'yticklabel2': ['Label 1', 'Label 2', 'Label 3']
})

# Initialize Matplotlib Axes
_, ax = plt.subplots()

# Call the function
ax, righttext_width = mdraw_yticklabel2(df, None, None, ax)

# Assertions
# Check if righttext_width is a float (or int, based on your implementation)
assert isinstance(righttext_width, (float, int)), "righttext_width is not a numeric value."

# Additional checks can be made for text properties like content, font size, and alignment
texts = [text for text in ax.get_children() if isinstance(text, plt.Text)]
for text, expected_label in zip(texts, df['yticklabel2']):
assert text.get_text() == expected_label, "Text label content does not match."

0 comments on commit ffa043e

Please sign in to comment.