ehrapy.plot.ols

ehrapy.plot.ols(adata=None, x=None, y=None, scatter_plot=True, ols_results=None, ols_color=None, xlabel=None, ylabel=None, figsize=None, lines=None, lines_color=None, lines_style=None, lines_label=None, xlim=None, ylim=None, show=None, ax=None, title=None, **kwds)[source]

Plots an Ordinary Least Squares (OLS) Model result, scatter plot, and line plot.

Parameters:
  • adata (AnnData | None) – AnnData object containing all observations.

  • x (str | None) – x coordinate, for scatter plotting.

  • y (str | None) – y coordinate, for scatter plotting.

  • scatter_plot (bool | None) – If True, show scatter plot. Defaults to True.

  • ols_results (list[RegressionResults] | None) – List of RegressionResults from ehrapy.tl.ols. Example: [result_1, result_2]

  • ols_color (list[str] | None) – List of colors for each ols_results. Example: [‘red’, ‘blue’].

  • xlabel (str | None) – The x-axis label text.

  • ylabel (str | None) – The y-axis label text.

  • figsize (tuple[float, float] | None) – Width, height in inches. Defaults to None.

  • lines (list[tuple[ndarray | float, ndarray | float]] | None) – List of Tuples of (slope, intercept) or (x, y). Plot lines by slope and intercept or data points. Example: plot two lines (y = x + 2 and y = 2*x + 1): [(1, 2), (2, 1)]

  • lines_color (list[str] | None) – List of colors for each line. Example: [‘red’, ‘blue’]

  • lines_style (list[str] | None) – List of line styles for each line. Example: [‘-’, ‘–‘]

  • lines_label (list[str] | None) – List of line labels for each line. Example: [‘Line1’, ‘Line2’]

  • xlim (tuple[float, float] | None) – Set the x-axis view limits. Required for only plotting lines using slope and intercept.

  • ylim (tuple[float, float] | None) – Set the y-axis view limits. Required for only plotting lines using slope and intercept.

  • show (bool | None) – Show the plot, do not return axis.

  • ax (Axes | None) – A matplotlib axes object. Only works if plotting a single component.

  • title (str | None) – Set the title of the plot.

Examples

>>> import ehrapy as ep
>>> adata = ep.dt.mimic_2(encoded=False)
>>> co2_lm_result = ep.tl.ols(
...     adata, var_names=["pco2_first", "tco2_first"], formula="tco2_first ~ pco2_first", missing="drop"
... ).fit()
>>> ep.pl.ols(
...     adata,
...     x="pco2_first",
...     y="tco2_first",
...     ols_results=[co2_lm_result],
...     ols_color=["red"],
...     xlabel="PCO2",
...     ylabel="TCO2",
... )
../../_images/ols_plot_1.png
>>> import ehrapy as ep
>>> adata = ep.dt.mimic_2(encoded=False)
>>> ep.pl.ols(adata, x='pco2_first', y='tco2_first', lines=[(0.25, 10), (0.3, 20)],
>>>           lines_color=['red', 'blue'], lines_style=['-', ':'], lines_label=['Line1', 'Line2'])
../../_images/ols_plot_2.png
>>> import ehrapy as ep
>>> ep.pl.ols(lines=[(0.25, 10), (0.3, 20)], lines_color=['red', 'blue'], lines_style=['-', ':'],
>>>           lines_label=['Line1', 'Line2'], xlim=(0, 150), ylim=(0, 50))
../../_images/ols_plot_3.png