From 569930e8bd40844ed7cf4ceff28c887db17e1bc6 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Thu, 26 Aug 2021 10:47:13 +0300 Subject: [PATCH 1/4] ppc_plot specify colors for each element --- arviz/plots/backends/bokeh/kdeplot.py | 1 + arviz/plots/backends/bokeh/ppcplot.py | 40 ++++++++++++++-------- arviz/plots/backends/matplotlib/ppcplot.py | 40 +++++++++++++--------- arviz/plots/ppcplot.py | 13 ++++--- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/arviz/plots/backends/bokeh/kdeplot.py b/arviz/plots/backends/bokeh/kdeplot.py index 5180249c5f..924325af63 100644 --- a/arviz/plots/backends/bokeh/kdeplot.py +++ b/arviz/plots/backends/bokeh/kdeplot.py @@ -150,6 +150,7 @@ def plot_kde( if label is not None: plot_kwargs.setdefault("legend_label", label) + print(plot_kwargs) if not rotated: line = ax.line(x, density, **plot_kwargs) else: diff --git a/arviz/plots/backends/bokeh/ppcplot.py b/arviz/plots/backends/bokeh/ppcplot.py index 44e3e3a94c..ea32c74767 100644 --- a/arviz/plots/backends/bokeh/ppcplot.py +++ b/arviz/plots/backends/bokeh/ppcplot.py @@ -23,7 +23,7 @@ def plot_ppc( pp_sample_ix, kind, alpha, - color, + colors, textsize, mean, observed, @@ -48,7 +48,7 @@ def plot_ppc( **backend_kwargs, } - color = vectorized_to_hex(color) + colors = vectorized_to_hex(colors) (figsize, *_, linewidth, markersize) = _scale_fig_size(figsize, textsize, rows, cols) if ax is None: @@ -95,7 +95,11 @@ def plot_ppc( pp_sampled_vals = pp_vals[pp_sample_ix] if kind == "kde": - plot_kwargs = {"line_color": color, "line_alpha": alpha, "line_width": 0.5 * linewidth} + plot_kwargs = { + "line_color": colors[0], + "line_alpha": alpha, + "line_width": 0.5 * linewidth, + } pp_densities = [] pp_xs = [] @@ -127,7 +131,7 @@ def plot_ppc( if dtype == "f": _, glyph = plot_kde( obs_vals, - plot_kwargs={"line_color": "black", "line_width": linewidth}, + plot_kwargs={"line_color": colors[1], "line_width": linewidth}, fill_kwargs={"alpha": 0}, ax=ax_i, backend="bokeh", @@ -143,7 +147,7 @@ def plot_ppc( step = ax_i.step( bin_edges, hist, - line_color="black", + line_color=colors[1], line_width=linewidth, mode="center", ) @@ -164,7 +168,7 @@ def plot_ppc( line = ax_i.line( new_x, new_d.mean(0), - color=color, + color=colors[2], line_dash="dashed", line_width=linewidth, ) @@ -177,7 +181,7 @@ def plot_ppc( step = ax_i.step( bin_edges, hist, - line_color=color, + line_color=colors[2], line_width=linewidth, line_dash="dashed", mode="center", @@ -193,7 +197,7 @@ def plot_ppc( if dtype == "f": glyph = ax_i.line( *_empirical_cdf(obs_vals), - line_color="black", + line_color=colors[1], line_width=linewidth, ) glyph.level = "overlay" @@ -202,7 +206,7 @@ def plot_ppc( else: step = ax_i.step( *_empirical_cdf(obs_vals), - line_color="black", + line_color=colors[1], line_width=linewidth, mode="center", ) @@ -217,7 +221,7 @@ def plot_ppc( list(pp_densities[::2]), list(pp_densities[1::2]), line_alpha=alpha, - line_color=color, + line_color=colors[0], line_width=linewidth, ) legend_it.append(("{} predictive".format(group.capitalize()), [multi_line])) @@ -225,7 +229,7 @@ def plot_ppc( label = "{} predictive mean".format(group.capitalize()) line = ax_i.line( *_empirical_cdf(pp_vals.flatten()), - color=color, + color=colors[2], line_dash="dashed", line_width=linewidth, ) @@ -238,7 +242,7 @@ def plot_ppc( _, glyph = plot_kde( pp_vals.flatten(), plot_kwargs={ - "line_color": color, + "line_color": colors[2], "line_dash": "dashed", "line_width": linewidth, }, @@ -257,7 +261,7 @@ def plot_ppc( step = ax_i.step( bin_edges, hist, - color=color, + color=colors[2], line_width=linewidth, line_dash="dashed", mode="center", @@ -279,7 +283,8 @@ def plot_ppc( glyph = ax_i.circle( obs_vals, obs_yvals, - fill_color="black", + line_color=colors[1], + fill_color=colors[1], size=markersize, line_alpha=alpha, ) @@ -293,7 +298,12 @@ def plot_ppc( if jitter: yvals += np.random.uniform(low=scale_low, high=scale_high, size=len(vals)) scatter = ax_i.scatter( - vals, yvals, fill_color=color, size=markersize, fill_alpha=alpha + vals, + yvals, + line_color=colors[0], + fill_color=colors[0], + size=markersize, + fill_alpha=alpha, ) all_scatter.append(scatter) diff --git a/arviz/plots/backends/matplotlib/ppcplot.py b/arviz/plots/backends/matplotlib/ppcplot.py index 854be0d52f..010e212dc4 100644 --- a/arviz/plots/backends/matplotlib/ppcplot.py +++ b/arviz/plots/backends/matplotlib/ppcplot.py @@ -27,7 +27,7 @@ def plot_ppc( pp_sample_ix, kind, alpha, - color, + colors, textsize, mean, observed, @@ -122,16 +122,16 @@ def plot_ppc( pp_sampled_vals = pp_vals[pp_sample_ix] if kind == "kde": - plot_kwargs = {"color": color, "alpha": alpha, "linewidth": 0.5 * linewidth} + plot_kwargs = {"color": colors[0], "alpha": alpha, "linewidth": 0.5 * linewidth} if dtype == "i": plot_kwargs["drawstyle"] = "steps-pre" - ax_i.plot([], color=color, label="{} predictive".format(group.capitalize())) + ax_i.plot([], color=colors[0], label="{} predictive".format(group.capitalize())) if observed: if dtype == "f": plot_kde( obs_vals, label="Observed", - plot_kwargs={"color": "k", "linewidth": linewidth, "zorder": 3}, + plot_kwargs={"color": colors[1], "linewidth": linewidth, "zorder": 3}, fill_kwargs={"alpha": 0}, ax=ax_i, legend=legend, @@ -144,7 +144,7 @@ def plot_ppc( bin_edges, hist, label="Observed", - color="k", + color=colors[1], linewidth=linewidth, zorder=3, drawstyle=plot_kwargs["drawstyle"], @@ -192,7 +192,7 @@ def plot_ppc( ax_i.plot( new_x, new_d.mean(0), - color=color, + color=colors[2], linestyle="--", linewidth=linewidth * 1.5, zorder=2, @@ -206,7 +206,7 @@ def plot_ppc( ax_i.plot( bin_edges, hist, - color=color, + color=colors[2], linewidth=linewidth * 1.5, label=label, zorder=2, @@ -221,7 +221,7 @@ def plot_ppc( if observed: ax_i.plot( *_empirical_cdf(obs_vals), - color="k", + color=colors[1], linewidth=linewidth, label="Observed", drawstyle=drawstyle, @@ -248,15 +248,15 @@ def plot_ppc( ax_i.plot( *pp_densities, alpha=alpha, - color=color, + color=colors[0], drawstyle=drawstyle, linewidth=linewidth ) - ax_i.plot([], color=color, label="Posterior predictive") + ax_i.plot([], color=colors[0], label="Posterior predictive") if mean: ax_i.plot( *_empirical_cdf(pp_vals.flatten()), - color=color, + color=colors[2], linestyle="--", linewidth=linewidth * 1.5, drawstyle=drawstyle, @@ -270,7 +270,7 @@ def plot_ppc( plot_kde( pp_vals.flatten(), plot_kwargs={ - "color": color, + "color": colors[2], "linestyle": "--", "linewidth": linewidth * 1.5, "zorder": 3, @@ -287,7 +287,7 @@ def plot_ppc( ax_i.plot( bin_edges, hist, - color=color, + color=colors[2], linewidth=linewidth * 1.5, label="Posterior predictive mean", zorder=3, @@ -312,7 +312,7 @@ def plot_ppc( obs_vals, obs_yvals, "o", - color="k", + color=colors[1], markersize=markersize, alpha=alpha, label="Observed", @@ -324,7 +324,7 @@ def plot_ppc( pp_sampled_vals, ax_i, kind=kind, - color=color, + color=colors[0], height=y_rows.mean() * 0.5, markersize=markersize, ) @@ -336,10 +336,16 @@ def plot_ppc( if jitter: yvals += np.random.uniform(low=scale_low, high=scale_high, size=len(vals)) ax_i.plot( - vals, yvals, "o", zorder=2, color=color, markersize=markersize, alpha=alpha + vals, + yvals, + "o", + zorder=2, + color=colors[0], + markersize=markersize, + alpha=alpha, ) - ax_i.plot([], color=color, marker="o", label="Posterior predictive") + ax_i.plot([], color=colors[0], marker="o", label="Posterior predictive") ax_i.set_yticks([]) diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py index a0378e8ca2..2f418c7bc3 100644 --- a/arviz/plots/ppcplot.py +++ b/arviz/plots/ppcplot.py @@ -19,7 +19,7 @@ def plot_ppc( alpha=None, mean=True, observed=True, - color="C0", + colors=None, grid=None, figsize=None, textsize=None, @@ -58,8 +58,10 @@ def plot_ppc( Whether or not to plot the mean posterior/prior predictive distribution. Defaults to True observed: bool, default True Whether or not to plot the observed data. - color: str - Valid matplotlib color. Defaults to C0 + color: list + List with valid matplotlib colors corresponding to the posterior/prior predictive + distribution, observed data and mean of the posterior/prior predictive distribution. + Defaults to ["C0", "k", "C1"] grid : tuple Number of rows and columns. Defaults to None, the rows and columns are automatically inferred. @@ -208,6 +210,9 @@ def plot_ppc( if kind.lower() not in ("kde", "cumulative", "scatter"): raise TypeError("`kind` argument must be either `kde`, `cumulative`, or `scatter`") + if colors is None: + colors = ["C0", "k", "C1"] + if data_pairs is None: data_pairs = {} @@ -308,7 +313,7 @@ def plot_ppc( pp_sample_ix=pp_sample_ix, kind=kind, alpha=alpha, - color=color, + colors=colors, jitter=jitter, textsize=textsize, mean=mean, From 0d663339f21e6ec220ca2455e852e523a28b0408 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Thu, 26 Aug 2021 10:57:44 +0300 Subject: [PATCH 2/4] remove print statement --- arviz/plots/backends/bokeh/kdeplot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/arviz/plots/backends/bokeh/kdeplot.py b/arviz/plots/backends/bokeh/kdeplot.py index 924325af63..5180249c5f 100644 --- a/arviz/plots/backends/bokeh/kdeplot.py +++ b/arviz/plots/backends/bokeh/kdeplot.py @@ -150,7 +150,6 @@ def plot_kde( if label is not None: plot_kwargs.setdefault("legend_label", label) - print(plot_kwargs) if not rotated: line = ax.line(x, density, **plot_kwargs) else: From 01c84c3a827d0fab27a20cae327c01ef74109de9 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Mon, 30 Aug 2021 11:31:05 +0300 Subject: [PATCH 3/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf2bd7acd6..cdc44e91f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Improve error messages in `stats.compare()`, and `var_name` parameter. ([1616](https://github.com/arviz-devs/arviz/pull/1616)) * Added ability to plot HDI contours to `plot_kde` with the new `hdi_probs` parameter. ([1665](https://github.com/arviz-devs/arviz/pull/1665)) * Add dtype parsing and setting in all Stan converters ([1632](https://github.com/arviz-devs/arviz/pull/1632)) +* Add option to specify colors for each element in ppc_plot ([1769](https://github.com/arviz-devs/arviz/pull/1769)) ### Maintenance and fixes * Fix conversion for numpyro models with ImproperUniform latent sites ([1713](https://github.com/arviz-devs/arviz/pull/1713)) From 436d1c2d42af5440c2bcc99d3e421525916c2e66 Mon Sep 17 00:00:00 2001 From: aloctavodia Date: Tue, 31 Aug 2021 08:16:59 +0300 Subject: [PATCH 4/4] add deprecation warning --- arviz/plots/ppcplot.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py index 2f418c7bc3..336ac26639 100644 --- a/arviz/plots/ppcplot.py +++ b/arviz/plots/ppcplot.py @@ -1,5 +1,6 @@ """Posterior/Prior predictive plot.""" import logging +import warnings from numbers import Integral import numpy as np @@ -19,6 +20,7 @@ def plot_ppc( alpha=None, mean=True, observed=True, + color=None, colors=None, grid=None, figsize=None, @@ -58,6 +60,8 @@ def plot_ppc( Whether or not to plot the mean posterior/prior predictive distribution. Defaults to True observed: bool, default True Whether or not to plot the observed data. + color: str + Valid matplotlib color. Defaults to C0 color: list List with valid matplotlib colors corresponding to the posterior/prior predictive distribution, observed data and mean of the posterior/prior predictive distribution. @@ -213,6 +217,10 @@ def plot_ppc( if colors is None: colors = ["C0", "k", "C1"] + if color is not None: + warnings.warn("color has been deprecated in favor of colors", FutureWarning) + colors[0] = color + if data_pairs is None: data_pairs = {}