Skip to content

Commit d362bc5

Browse files
authored
Merge pull request #98 from LSYS/juancq
* From #73 by @juancq. * Warn about duplicated `varlabel` (closes #76, closes #81). * Add test that above warning works. * Add known issues about duplicated `varlabel` (closes #76, closes #81) and PyCharm (closes #80).
2 parents 55c76f1 + 3519d74 commit d362bc5

File tree

7 files changed

+71
-5
lines changed

7 files changed

+71
-5
lines changed

README.md

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

313315
<!----------------- BACKGROUND AND ADDITIONAL RESOURCES ----------------->

examples/subplot_example.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import matplotlib.gridspec as gridspec
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
import pandas as pd
5+
6+
import forestplot as fp
7+
8+
# fill in
9+
df = pd.read_csv('test.csv')
10+
11+
headers = ['Header 1','Header 2','Header 3','Header 4','Header 5']
12+
header_short = ['h1', 'h2', 'h3', 'h4', 'h5']
13+
14+
fig, axarr = plt.subplots(2, 3, figsize=(20,20), sharey=True)
15+
fig.tight_layout(h_pad=2)
16+
17+
k = 0
18+
for i in range(2):
19+
for j in range(3):
20+
col = header_short[k]
21+
ax = fp.forestplot(df, ax=axarr[i,j], estimate=col+'1', ll=col+'2', hl=col+'3',
22+
varlabel='label',
23+
ci_report=False,
24+
**{"marker": "D", # set maker symbol as diamond
25+
"markersize": 35, # adjust marker size
26+
"xlinestyle": (0, (10, 5)), # long dash for x-reference line
27+
"xlinecolor": ".1", # gray color for x-reference line
28+
"xtick_size": 12, # adjust x-ticker fontsize
29+
"fontsize": 14,
30+
} )
31+
if j > 0:
32+
ax.axes.get_yaxis().set_visible(False)
33+
ax.set_xlim(-.09, .09)
34+
ax.set_title(headers[k])#, loc='left')
35+
k += 1
36+
if k >= 5:
37+
break
38+
39+
axarr[-1,-1].axis('off')
40+
41+
plt.savefig('test.png', bbox_inches='tight', dpi=300)

forestplot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""State version and import user-facing functions."""
2-
VERSION = (0, 3, 2)
2+
VERSION = (0, 3, 3)
33

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

forestplot/arg_validators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ def check_data(
221221
"Duplicates found in variable labels ('varlabel') and group labels ('groupvar'). Formatting of y-axis labels may lead to unexpected errors."
222222
)
223223

224+
if len(dataframe) != dataframe[varlabel].dropna().nunique():
225+
warnings.warn(
226+
"Duplicates found in variable labels ('varlabel'). Plot may have errors."
227+
)
228+
224229
return dataframe
225230

226231

forestplot/plot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def forestplot(
7878
return_df: bool = False,
7979
preprocess: bool = True,
8080
table: bool = False,
81+
ax: Optional[Axes] = None,
8182
**kwargs: Any,
8283
) -> Axes:
8384
"""
@@ -218,6 +219,7 @@ def forestplot(
218219
yticker2=yticker2,
219220
color_alt_rows=color_alt_rows,
220221
table=table,
222+
ax=ax,
221223
**kwargs,
222224
)
223225
return (_local_df, ax) if return_df else ax
@@ -346,6 +348,7 @@ def _make_forestplot(
346348
yticker2: Optional[str],
347349
color_alt_rows: bool,
348350
figsize: Union[Tuple, List],
351+
ax: Axes,
349352
despine: bool = True,
350353
table: bool = False,
351354
**kwargs: Any,
@@ -357,7 +360,8 @@ def _make_forestplot(
357360
-------
358361
Matplotlib Axes object.
359362
"""
360-
_, ax = plt.subplots(figsize=figsize, facecolor="white")
363+
if not ax:
364+
_, ax = plt.subplots(figsize=figsize, facecolor="white")
361365
ax = draw_ci(
362366
dataframe=dataframe,
363367
estimate=estimate,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
install_requires = ["pandas", "numpy", "matplotlib", "matplotlib-inline<=0.1.3"]
1111
setup(
1212
name="forestplot",
13-
version="0.3.2",
13+
version="0.3.3",
1414
license="MIT",
1515
author="Lucas Shen",
1616
author_email="lucas@lucasshen.com",

tests/test_arg_validators.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_check_data():
105105
check_data(
106106
dataframe=_df,
107107
estimate="estimate",
108-
varlabel="moerror",
108+
varlabel="estimate",
109109
annote=["ci_range"],
110110
)
111111

@@ -147,7 +147,7 @@ def test_check_data():
147147
check_data(
148148
dataframe=_df,
149149
estimate="estimate",
150-
varlabel="moerror",
150+
varlabel="estimate",
151151
rightannote=["ci_range"],
152152
)
153153

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

181+
##########################################################################
182+
## Check that warning for duplicated label works
183+
##########################################################################
184+
duplicated_string = ["a", "a", "c"]
185+
numeric = [-1, 2, 3.0]
186+
187+
# Assert that assertion for numeric type for estimate works
188+
_df = pd.DataFrame({"varlabel": duplicated_string, "estimate": numeric})
189+
with pytest.warns(UserWarning) as user_warning:
190+
check_data(dataframe=_df, estimate="estimate", varlabel="varlabel")
191+
assert "Duplicates found in variable labels ('varlabel'). Plot may have errors." in str(
192+
user_warning[0].message
193+
)
194+
181195

182196
def test_check_iterables_samelen():
183197
thresholds = (0.01, 0.05, 0.1)

0 commit comments

Comments
 (0)