From 88f7e2af30f9c52661c99d97b63df545b2d2c170 Mon Sep 17 00:00:00 2001 From: Oriol Abril-Pla Date: Sat, 10 Jun 2023 10:23:05 +0200 Subject: [PATCH] adapt histograms to float/int (#2247) * adapt histograms to float/int * changelog and black * fix logic in bokeh * define ticks from bin edges after calling hist * black --- CHANGELOG.md | 1 + arviz/plots/backends/bokeh/distplot.py | 3 ++- arviz/plots/backends/matplotlib/distplot.py | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77b1ae87dd..d55649c631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Maintenance and fixes - Fixes for creating numpy object array ([2233](https://github.com/arviz-devs/arviz/pull/2233) and [2239](https://github.com/arviz-devs/arviz/pull/2239)) +- Adapt histograms generated by plot_dist to input dtype ([2247](https://github.com/arviz-devs/arviz/pull/2247)) ### Deprecation diff --git a/arviz/plots/backends/bokeh/distplot.py b/arviz/plots/backends/bokeh/distplot.py index 4f1956110b..517f6fa92f 100644 --- a/arviz/plots/backends/bokeh/distplot.py +++ b/arviz/plots/backends/bokeh/distplot.py @@ -141,6 +141,8 @@ def _histplot_bokeh_op(values, values2, rotated, ax, hist_kwargs, is_circular): if hist_kwargs.pop("cumulative", False): hist = np.cumsum(hist) hist /= hist[-1] + if values.dtype.kind == "i": + edges = edges.astype(float) - 0.5 if is_circular: @@ -174,7 +176,6 @@ def _histplot_bokeh_op(values, values2, rotated, ax, hist_kwargs, is_circular): ) ax = set_bokeh_circular_ticks_labels(ax, hist, labels) - elif rotated: ax.quad(top=edges[:-1], bottom=edges[1:], left=0, right=hist, **hist_kwargs) else: diff --git a/arviz/plots/backends/matplotlib/distplot.py b/arviz/plots/backends/matplotlib/distplot.py index bc0fdf2e66..8d8257acdf 100644 --- a/arviz/plots/backends/matplotlib/distplot.py +++ b/arviz/plots/backends/matplotlib/distplot.py @@ -67,7 +67,6 @@ def plot_dist( hist_kwargs.setdefault("color", color) hist_kwargs.setdefault("label", label) hist_kwargs.setdefault("rwidth", 0.9) - hist_kwargs.setdefault("align", "left") hist_kwargs.setdefault("density", True) if rotated: @@ -151,12 +150,22 @@ def _histplot_mpl_op(values, values2, rotated, ax, hist_kwargs, is_circular): if bins is None: bins = get_bins(values) + if values.dtype.kind == "i": + hist_kwargs.setdefault("align", "left") + else: + hist_kwargs.setdefault("align", "mid") + n, bins, _ = ax.hist(np.asarray(values).flatten(), bins=bins, **hist_kwargs) + if values.dtype.kind == "i": + ticks = bins[:-1] + else: + ticks = (bins[1:] + bins[:-1]) / 2 + if rotated: - ax.set_yticks(bins[:-1]) + ax.set_yticks(ticks) elif not is_circular: - ax.set_xticks(bins[:-1]) + ax.set_xticks(ticks) if is_circular: ax.set_ylim(0, 1.5 * n.max())