arviz.plot_posterior

arviz.plot_posterior(data, var_names=None, transform=None, coords=None, figsize=None, textsize=None, credible_interval='auto', multimodal=False, round_to: Union[int, NoneType] = None, point_estimate='auto', group='posterior', rope=None, ref_val=None, kind='kde', bw=4.5, bins=None, ax=None, backend=None, backend_kwargs=None, show=None, **kwargs)[source]

Plot Posterior densities in the style of John K. Kruschke’s book.

Parameters
dataobj

Any object that can be converted to an az.InferenceData object Refer to documentation of az.convert_to_dataset for details

var_nameslist of variable names

Variables to be plotted, two variables are required.

transformcallable

Function to transform data (defaults to None i.e.the identity function)

coordsmapping, optional

Coordinates of var_names to be plotted. Passed to Dataset.sel

figsizetuple

Figure size. If None it will be defined automatically.

textsize: float

Text size scaling factor for labels, titles and lines. If None it will be autoscaled based on figsize.

credible_intervalfloat, optional

Credible intervals. Defaults to 0.94. Use None to hide the credible interval

multimodalbool

If true (default) it may compute more than one credible interval if the distribution is multimodal and the modes are well separated.

round_toint, optional

Controls formatting of floats. Defaults to 2 or the integer part, whichever is bigger.

point_estimateOptional[str]

Plot point estimate per variable. Values should be ‘mean’, ‘median’, ‘mode’ or None. Defaults to ‘auto’ i.e. it falls back to default set in rcParams.

groupstr, optional

Specifies which InferenceData group should be plotted. Defaults to ‘posterior’.

rope: tuple or dictionary of tuples

Lower and upper values of the Region Of Practical Equivalence. If a list is provided, its length should match the number of variables.

ref_val: float or dictionary of floats

display the percentage below and above the values in ref_val. Must be None (default), a constant, a list or a dictionary like see an example below. If a list is provided, its length should match the number of variables.

kind: str

Type of plot to display (kde or hist) For discrete variables this argument is ignored and a histogram is always used.

bwfloat

Bandwidth scaling factor for the KDE. Should be larger than 0. The higher this number the smoother the KDE will be. Defaults to 4.5 which is essentially the same as the Scott’s rule of thumb (the default rule used by SciPy). Only works if kind == kde.

binsinteger or sequence or ‘auto’, optional

Controls the number of bins, accepts the same keywords matplotlib.hist() does. Only works if kind == hist. If None (default) it will use auto for continuous variables and range(xmin, xmax + 1) for discrete variables.

ax: numpy array-like of matplotlib axes or bokeh figures, optional

A 2D array of locations into which to plot the densities. If not supplied, Arviz will create its own array of plot areas (and return it).

backend: str, optional

Select plotting backend {“matplotlib”,”bokeh”}. Default “matplotlib”.

backend_kwargs: bool, optional

These are kwargs specific to the backend being used. For additional documentation check the plotting method of the backend.

showbool, optional

Call backend show function.

**kwargs

Passed as-is to plt.hist() or plt.plot() function depending on the value of kind.

Returns
axesmatplotlib axes or bokeh figures

Examples

Show a default kernel density plot following style of John Kruschke

>>> import arviz as az
>>> data = az.load_arviz_data('centered_eight')
>>> az.plot_posterior(data)
../_images/arviz-plot_posterior-1.png

Plot subset variables by specifying variable name exactly

>>> az.plot_posterior(data, var_names=['mu'])
../_images/arviz-plot_posterior-2.png

Plot Region of Practical Equivalence (rope) for all distributions

>>> az.plot_posterior(data, var_names=['mu', 'theta'], rope=(-1, 1))
../_images/arviz-plot_posterior-3.png

Plot Region of Practical Equivalence for selected distributions

>>> rope = {'mu': [{'rope': (-2, 2)}], 'theta': [{'school': 'Choate', 'rope': (2, 4)}]}
>>> az.plot_posterior(data, var_names=['mu', 'theta'], rope=rope)
../_images/arviz-plot_posterior-4.png

Add reference lines

>>> az.plot_posterior(data, var_names=['mu', 'theta'], ref_val=0)
../_images/arviz-plot_posterior-5.png

Show point estimate of distribution

>>> az.plot_posterior(data, var_names=['mu', 'theta'], point_estimate='mode')
../_images/arviz-plot_posterior-6.png

Show reference values using variable names and coordinates

>>> az.plot_posterior(data, ref_val= {"theta": [{"school": "Deerfield", "ref_val": 4},
...                                             {"school": "Choate", "ref_val": 3}]})
../_images/arviz-plot_posterior-7.png

Show reference values using a list

>>> az.plot_posterior(data, ref_val=[1] + [5] * 8 + [1])
../_images/arviz-plot_posterior-8.png

Plot posterior as a histogram

>>> az.plot_posterior(data, var_names=['mu'], kind='hist')
../_images/arviz-plot_posterior-9.png

Change size of credible interval

>>> az.plot_posterior(data, var_names=['mu'], credible_interval=.75)
../_images/arviz-plot_posterior-10.png