(gallery_lm)=

# Linear model plot

Posterior predictive and mean plots for regression-like data. The `plot_lm` function
visualizes credible intervals around predictions alongside observed data points.



::::::{tab-set}
:class: full-width
:sync-group: backend

:::::{tab-item} Matplotlib
:sync: matplotlib

![Matplotlib version of plot_lm](_images/plot_lm.png)

:::::

:::::{tab-item} Bokeh
:sync: bokeh

```{bokeh-plot}
:source-position: none

from bokeh.plotting import show

import numpy as np
from arviz_base import from_dict

import arviz_plots as azp

azp.style.use("arviz-variat")

np.random.seed(42)
x_data = np.random.normal(0, 1, 100)
y_data = 2 + x_data * 0.5 + np.random.normal(0, 0.5, 100)
y_data_rep = np.random.normal(2 + x_data * 0.5, 0.5, (4, 200, 100))

dt = from_dict(
{
"posterior_predictive": {"y": y_data_rep},
"observed_data": {"y": y_data},
"constant_data": {"x": x_data},
},
dims={"y": ["obs_id"], "x": ["obs_id"]},
coords={"obs_id": range(100)},
)

pc = azp.plot_lm(
dt,
backend="bokeh",  # change to preferred backend
)


# for some reason the bokeh plot extension needs explicit use of show
show(pc.viz["figure"].item() if pc.viz["figure"].item() is not None else pc.viz["plot"].item())
```

Link to this page with the [bokeh tab selected](https://arviz-plots.readthedocs.io/en/latest//gallery/plot_lm.html?backend=bokeh#synchronised-tabs)
:::::

:::::{tab-item} Plotly
:sync: plotly

```{jupyter-execute}
:hide-code:

import numpy as np
from arviz_base import from_dict

import arviz_plots as azp

azp.style.use("arviz-variat")

np.random.seed(42)
x_data = np.random.normal(0, 1, 100)
y_data = 2 + x_data * 0.5 + np.random.normal(0, 0.5, 100)
y_data_rep = np.random.normal(2 + x_data * 0.5, 0.5, (4, 200, 100))

dt = from_dict(
{
"posterior_predictive": {"y": y_data_rep},
"observed_data": {"y": y_data},
"constant_data": {"x": x_data},
},
dims={"y": ["obs_id"], "x": ["obs_id"]},
coords={"obs_id": range(100)},
)

pc = azp.plot_lm(
dt,
backend="plotly",  # change to preferred backend
)
pc.show()
```

Link to this page with the [plotly tab selected](https://arviz-plots.readthedocs.io/en/latest//gallery/plot_lm.html?backend=plotly#synchronised-tabs)
:::::
::::::

```{literalinclude} _scripts/plot_lm.py
:emphasize-lines: 25
```



:::{seealso}
API Documentation: {func}`~arviz_plots.plot_lm`

:::



## Other examples with `plot_lm`

```{eval-rst}
.. minigallery:: plot_lm
```


:::{div} example-plot-download
{download}`Download Python Source Code: plot_lm.py<_scripts/plot_lm.py>`
:::
