Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use get_bins function to compute bins for density and posterior plot #1049

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Add `skipna` argument to `hpd` and `summary` (#1035)

### Maintenance and fixes
* Fixed bug in density and posterior plot bin computation (#1049)
* Fixed bug in density plot ax argument (#1049)
* Fixed bug in extracting prior samples for cmdstanpy (#979)
* Fix erroneous warning in traceplot (#989)
* Correct bfmi denominator (#991)
Expand Down
3 changes: 2 additions & 1 deletion arviz/plots/backends/bokeh/densityplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_create_axes_grid,
calculate_point_estimate,
_fast_kde,
get_bins,
)
from ....stats import hpd
from ....stats.stats_utils import histogram
Expand Down Expand Up @@ -159,7 +160,7 @@ def _d_helper(

else:
xmin, xmax = hpd(vec, credible_interval, multimodal=False)
bins = range(xmin, xmax + 2)
bins = get_bins(vec)

_, hist, edges = histogram(vec, bins=bins)

Expand Down
5 changes: 2 additions & 3 deletions arviz/plots/backends/bokeh/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
format_sig_figs,
round_num,
calculate_point_estimate,
get_bins,
)
from ....stats import hpd

Expand Down Expand Up @@ -245,9 +246,7 @@ def format_axes():
else:
if bins is None:
if values.dtype.kind == "i":
xmin = values.min()
xmax = values.max()
bins = range(xmin, xmax + 2)
bins = get_bins(values)
else:
bins = "auto"
kwargs.setdefault("align", "left")
Expand Down
27 changes: 16 additions & 11 deletions arviz/plots/backends/matplotlib/densityplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
_create_axes_grid,
calculate_point_estimate,
_fast_kde,
get_bins,
)


Expand Down Expand Up @@ -37,16 +38,20 @@ def plot_density(
show,
):
"""Matplotlib densityplot."""
_, ax = _create_axes_grid(
length_plotters,
rows,
cols,
figsize=figsize,
squeeze=False,
backend="matplotlib",
backend_kwargs=backend_kwargs,
)
axis_map = {label: ax_ for label, ax_ in zip(all_labels, ax.flatten())}
if ax is None:
_, ax = _create_axes_grid(
length_plotters,
rows,
cols,
figsize=figsize,
squeeze=False,
backend="matplotlib",
backend_kwargs=backend_kwargs,
)
else:
ax = np.atleast_2d(ax)

axis_map = {label: ax_ for label, ax_ in zip(all_labels, np.ravel(ax))}

for m_idx, plotters in enumerate(to_plot):
for var_name, selection, values in plotters:
Expand Down Expand Up @@ -150,7 +155,7 @@ def _d_helper(

else:
xmin, xmax = hpd(vec, credible_interval, multimodal=False)
bins = range(xmin, xmax + 2)
bins = get_bins(vec)
if outline:
ax.hist(vec, bins=bins, color=color, histtype="step", align="left")
if shade:
Expand Down
3 changes: 2 additions & 1 deletion arviz/plots/backends/matplotlib/posteriorplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
format_sig_figs,
round_num,
calculate_point_estimate,
get_bins,
)


Expand Down Expand Up @@ -258,7 +259,7 @@ def format_axes():
if values.dtype.kind == "i":
xmin = values.min()
xmax = values.max()
bins = range(xmin, xmax + 2)
bins = get_bins(values)
ax.set_xlim(xmin - 0.5, xmax + 0.5)
else:
bins = "auto"
Expand Down