diff --git a/forestplot/graph_utils.py b/forestplot/graph_utils.py index e9b2b58..368cd97 100644 --- a/forestplot/graph_utils.py +++ b/forestplot/graph_utils.py @@ -1,6 +1,6 @@ """Holds functions to draw the plot.""" import warnings -from typing import Any, Optional, Sequence, Tuple, Union +from typing import Any, List, Optional, Sequence, Tuple, Union import matplotlib.pyplot as plt import pandas as pd @@ -544,6 +544,7 @@ def format_xticks( hl: str, xticks: Optional[Union[list, range]], ax: Axes, + xlim: Optional[Union[Tuple, List]] = None, **kwargs: Any ) -> Axes: """ @@ -596,6 +597,8 @@ def format_xticks( ax.set_xticklabels(xticklabels) for xticklab in ax.get_xticklabels(): xticklab.set_fontfamily("sans-serif") + if xlim: + ax.set_xlim(xlim[0], xlim[1]) return ax diff --git a/forestplot/mplot.py b/forestplot/mplot.py index bcc29d0..dd2a4ac 100644 --- a/forestplot/mplot.py +++ b/forestplot/mplot.py @@ -79,6 +79,7 @@ def mforestplot( preprocess: bool = True, table: bool = False, legend: bool = True, + xlim: Optional[Union[Tuple, List]] = None, **kwargs: Any, ) -> Axes: """ @@ -199,6 +200,7 @@ def mforestplot( color_alt_rows=color_alt_rows, table=table, legend=legend, + xlim=xlim, **kwargs, ) if return_df: @@ -312,6 +314,7 @@ def _make_mforestplot( color_alt_rows: bool = False, table: bool = False, legend: bool = True, + xlim: Optional[Union[Tuple, List]] = None, **kwargs: Any, ) -> Axes: if models is None: @@ -348,7 +351,14 @@ def _make_mforestplot( ) format_xticks( - dataframe=dataframe, estimate=estimate, ll=ll, hl=hl, xticks=xticks, ax=ax, **kwargs + dataframe=dataframe, + estimate=estimate, + ll=ll, + hl=hl, + xticks=xticks, + xlim=xlim, + ax=ax, + **kwargs, ) mdraw_ref_xline( ax=ax, @@ -410,4 +420,5 @@ def _make_mforestplot( negative_padding = 0.5 ax.set_ylim(-0.5, ax.get_ylim()[1] - negative_padding) despineplot(despine=despine, ax=ax) + return ax