Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juancq #98

Merged
merged 8 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ More fined-grained control for base plot options (eg font sizes, marker colors)
* Left-flushing of annotations relies on the `monospace` font.
* Plot may give strange behavior for few rows of data (six rows or fewer. [see this issue](https://github.com/LSYS/forestplot/issues/52))
* Plot can get cluttered with too many variables/rows (~30 onwards)
* Not tested with PyCharm (#80).
* Duplicated `varlabel` may lead to unexpected results (see #76, #81). `mplot` for grouped models could be useful for such cases (see #59, WIP).
<p align="right">(<a href="#top">back to top</a>)</p>

<!----------------- BACKGROUND AND ADDITIONAL RESOURCES ----------------->
Expand Down
41 changes: 41 additions & 0 deletions examples/subplot_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import forestplot as fp

# fill in
df = pd.read_csv('test.csv')

headers = ['Header 1','Header 2','Header 3','Header 4','Header 5']
header_short = ['h1', 'h2', 'h3', 'h4', 'h5']

fig, axarr = plt.subplots(2, 3, figsize=(20,20), sharey=True)
fig.tight_layout(h_pad=2)

k = 0
for i in range(2):
for j in range(3):
col = header_short[k]
ax = fp.forestplot(df, ax=axarr[i,j], estimate=col+'1', ll=col+'2', hl=col+'3',
varlabel='label',
ci_report=False,
**{"marker": "D", # set maker symbol as diamond
"markersize": 35, # adjust marker size
"xlinestyle": (0, (10, 5)), # long dash for x-reference line
"xlinecolor": ".1", # gray color for x-reference line
"xtick_size": 12, # adjust x-ticker fontsize
"fontsize": 14,
} )
if j > 0:
ax.axes.get_yaxis().set_visible(False)
ax.set_xlim(-.09, .09)
ax.set_title(headers[k])#, loc='left')
k += 1
if k >= 5:
break

axarr[-1,-1].axis('off')

plt.savefig('test.png', bbox_inches='tight', dpi=300)
2 changes: 1 addition & 1 deletion forestplot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""State version and import user-facing functions."""
VERSION = (0, 3, 2)
VERSION = (0, 3, 3)

__version__ = ".".join(map(str, VERSION))

Expand Down
5 changes: 5 additions & 0 deletions forestplot/arg_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ def check_data(
"Duplicates found in variable labels ('varlabel') and group labels ('groupvar'). Formatting of y-axis labels may lead to unexpected errors."
)

if len(dataframe) != dataframe[varlabel].dropna().nunique():
warnings.warn(
"Duplicates found in variable labels ('varlabel'). Plot may have errors."
)

return dataframe


Expand Down
6 changes: 5 additions & 1 deletion forestplot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def forestplot(
return_df: bool = False,
preprocess: bool = True,
table: bool = False,
ax: Optional[Axes] = None,
**kwargs: Any,
) -> Axes:
"""
Expand Down Expand Up @@ -218,6 +219,7 @@ def forestplot(
yticker2=yticker2,
color_alt_rows=color_alt_rows,
table=table,
ax=ax,
**kwargs,
)
return (_local_df, ax) if return_df else ax
Expand Down Expand Up @@ -346,6 +348,7 @@ def _make_forestplot(
yticker2: Optional[str],
color_alt_rows: bool,
figsize: Union[Tuple, List],
ax: Axes,
despine: bool = True,
table: bool = False,
**kwargs: Any,
Expand All @@ -357,7 +360,8 @@ def _make_forestplot(
-------
Matplotlib Axes object.
"""
_, ax = plt.subplots(figsize=figsize, facecolor="white")
if not ax:
_, ax = plt.subplots(figsize=figsize, facecolor="white")
ax = draw_ci(
dataframe=dataframe,
estimate=estimate,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
install_requires = ["pandas", "numpy", "matplotlib", "matplotlib-inline<=0.1.3"]
setup(
name="forestplot",
version="0.3.2",
version="0.3.3",
license="MIT",
author="Lucas Shen",
author_email="lucas@lucasshen.com",
Expand Down
18 changes: 16 additions & 2 deletions tests/test_arg_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_check_data():
check_data(
dataframe=_df,
estimate="estimate",
varlabel="moerror",
varlabel="estimate",
annote=["ci_range"],
)

Expand Down Expand Up @@ -147,7 +147,7 @@ def test_check_data():
check_data(
dataframe=_df,
estimate="estimate",
varlabel="moerror",
varlabel="estimate",
rightannote=["ci_range"],
)

Expand Down Expand Up @@ -178,6 +178,20 @@ def test_check_data():
== "Annotation headers are provided but no columns provided ('annote')."
)

##########################################################################
## Check that warning for duplicated label works
##########################################################################
duplicated_string = ["a", "a", "c"]
numeric = [-1, 2, 3.0]

# Assert that assertion for numeric type for estimate works
_df = pd.DataFrame({"varlabel": duplicated_string, "estimate": numeric})
with pytest.warns(UserWarning) as user_warning:
check_data(dataframe=_df, estimate="estimate", varlabel="varlabel")
assert "Duplicates found in variable labels ('varlabel'). Plot may have errors." in str(
user_warning[0].message
)


def test_check_iterables_samelen():
thresholds = (0.01, 0.05, 0.1)
Expand Down
Loading