Skip to content

Commit

Permalink
add plot.density_type
Browse files Browse the repository at this point in the history
  • Loading branch information
OriolAbril committed Mar 26, 2021
1 parent 5a3cfc4 commit 686dad7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions arviz/plots/backends/bokeh/energyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def plot_energy(
fill_kwargs = {} if fill_kwargs is None else fill_kwargs
plot_kwargs = {} if plot_kwargs is None else plot_kwargs
plot_kwargs.setdefault("line_width", line_width)
if kind in {"hist", "histogram"}:
if kind=="hist":
legend = False

if ax is None:
Expand Down Expand Up @@ -103,7 +103,7 @@ def plot_energy(
)
)

elif kind in {"hist", "histogram"}:
elif kind=="hist":
hist_kwargs = plot_kwargs.copy()
hist_kwargs.update(**fill_kwargs)

Expand Down
4 changes: 2 additions & 2 deletions arviz/plots/backends/matplotlib/energyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def plot_energy(
_, ax = create_axes_grid(1, backend_kwargs=backend_kwargs)

fill_kwargs = matplotlib_kwarg_dealiaser(fill_kwargs, "hexbin")
types = "hist" if kind in {"hist", "histogram"} else "plot"
types = "hist" if kind=="hist" else "plot"
plot_kwargs = matplotlib_kwarg_dealiaser(plot_kwargs, types)

_colors = [
Expand Down Expand Up @@ -82,7 +82,7 @@ def plot_energy(
ax=ax,
legend=False,
)
elif kind in {"hist", "histogram"}:
elif kind=="hist":
for alpha, color, label, value in series:
ax.hist(
value.flatten(),
Expand Down
7 changes: 4 additions & 3 deletions arviz/plots/distplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def plot_dist(
color : string
valid matplotlib color
kind : string
By default ("auto") continuous variables are plotted using KDEs and discrete ones using
histograms. To override this use "hist" to plot histograms and "kde" for KDEs
By default ("auto") continuous variables will use the kind defined by rcParam
``plot.density_kind`` and discrete ones will use histograms.
To override this use "hist" to plot histograms and "kde" for KDEs
cumulative : bool
If true plot the estimated cumulative distribution function. Defaults to False.
Ignored for 2D KDE
Expand Down Expand Up @@ -172,7 +173,7 @@ def plot_dist(
raise TypeError('Invalid "kind":{}. Select from {{"auto","kde","hist"}}'.format(kind))

if kind == "auto":
kind = "hist" if values.dtype.kind == "i" else "kde"
kind = "hist" if values.dtype.kind == "i" else rcParams["plot.density_kind"]

dist_plot_args = dict(
# User Facing API that can be simplified
Expand Down
17 changes: 15 additions & 2 deletions arviz/plots/energyplot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Plot energy transition distribution in HMC inference."""
import warnings

from ..data import convert_to_dataset
from ..rcparams import rcParams
from .plot_utils import get_plotting_function


def plot_energy(
data,
kind="kde",
kind=None,
bfmi=True,
figsize=None,
legend=True,
Expand All @@ -30,7 +32,7 @@ def plot_energy(
data : xarray dataset, or object that can be converted (must represent
`sample_stats` and have an `energy` variable)
kind : str
Type of plot to display {"kde", "histogram")
Type of plot to display {"kde", "hist")
bfmi : bool
If True add to the plot the value of the estimated Bayesian fraction of missing information
figsize : tuple
Expand Down Expand Up @@ -90,6 +92,17 @@ def plot_energy(
"""
energy = convert_to_dataset(data, group="sample_stats").energy.values

if kind == "histogram":
warnings.warn(
"kind histogram will be deprecated in a future release. Use `hist` "
"or set rcParam `plot.density_kind` to `hist`",
FutureWarning
)
kind = "hist"

if kind is None:
kind = rcParams["plot.density_kind"]

plot_energy_kwargs = dict(
ax=ax,
energy=energy,
Expand Down
7 changes: 5 additions & 2 deletions arviz/plots/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def plot_posterior(
ref_val=None,
rope_color="C2",
ref_val_color="C1",
kind="kde",
kind=None,
bw="default",
circular=False,
bins=None,
Expand Down Expand Up @@ -92,7 +92,7 @@ def plot_posterior(
Specifies the color of the displayed percentage
kind: str
Type of plot to display (kde or hist) For discrete variables this argument is ignored and
a histogram is always used.
a histogram is always used. Defaults to rcParam ``plot.density_kind``
bw: float or str, optional
If numeric, indicates the bandwidth and must be positive.
If str, indicates the method to estimate the bandwidth and must be
Expand Down Expand Up @@ -235,6 +235,9 @@ def plot_posterior(
elif point_estimate not in {"mean", "median", "mode", None}:
raise ValueError("The value of point_estimate must be either mean, median, mode or None.")

if kind is None:
kind = rcParams["plot.density_kind"]

plotters = filter_plotters_list(
list(xarray_var_iter(get_coords(data, coords), var_names=var_names, combined=True)),
"plot_posterior",
Expand Down
1 change: 1 addition & 0 deletions arviz/rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def validate_iterable(value):
"data.log_likelihood": (True, _validate_boolean),
"data.save_warmup": (False, _validate_boolean),
"plot.backend": ("matplotlib", _make_validate_choice({"matplotlib", "bokeh"})),
"plot.density_kind": ("kde", _make_validate_choice({"kde", "hist"})),
"plot.max_subplots": (40, _validate_positive_int_or_none),
"plot.point_estimate": (
"mean",
Expand Down
1 change: 1 addition & 0 deletions arvizrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data.save_warmup : false # save warmup iterations, one of "true", "
### PLOT ###
# rcParams related with plotting functions
plot.backend : matplotlib # One of "bokeh", "matplotlib"
plot.density_kind : kde # One of "kde", "hist"
plot.max_subplots : 40 # Maximum number of subplots.
plot.point_estimate : mean # One of "mean", "median", "mode" or "None"

Expand Down

0 comments on commit 686dad7

Please sign in to comment.