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

Remove varname from legend in plotppc #1559

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Maintenance and fixes
* Updated `from_cmdstan` and `from_numpyro` converter to follow schema convention ([1541](https://github.com/arviz-devs/arviz/pull/1541) and [1525](https://github.com/arviz-devs/arviz/pull/1525))
* Fix calculation of mode as point estimate ([1552](https://github.com/arviz-devs/arviz/pull/1552))
* Remove variable name from legend in posterior predictive plot ([1559](https://github.com/arviz-devs/arviz/pull/1559))

### Deprecation
* Removed Geweke diagnostic ([1545](https://github.com/arviz-devs/arviz/pull/1545))
Expand Down
26 changes: 11 additions & 15 deletions arviz/plots/backends/matplotlib/ppcplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ def plot_ppc(
plot_kwargs = {"color": color, "alpha": alpha, "linewidth": 0.5 * linewidth}
if dtype == "i":
plot_kwargs["drawstyle"] = "steps-pre"
ax_i.plot(
[], color=color, label="{} predictive {}".format(group.capitalize(), pp_var_name)
)
ax_i.plot([], color=color, label="{} predictive".format(group.capitalize()))
if observed:
if dtype == "f":
plot_kde(
obs_vals,
label="Observed {}".format(var_name),
label="Observed",
plot_kwargs={"color": "k", "linewidth": linewidth, "zorder": 3},
fill_kwargs={"alpha": 0},
ax=ax_i,
Expand All @@ -144,7 +142,7 @@ def plot_ppc(
ax_i.plot(
bin_edges,
hist,
label="Observed {}".format(var_name),
label="Observed",
color="k",
linewidth=linewidth,
zorder=3,
Expand Down Expand Up @@ -179,7 +177,7 @@ def plot_ppc(
ax_i.plot(x_s, y_s, **plot_kwargs)

if mean:
label = "{} predictive mean {}".format(group.capitalize(), pp_var_name)
label = "{} predictive mean".format(group.capitalize())
if dtype == "f":
rep = len(pp_densities)
len_density = len(pp_densities[0])
Expand Down Expand Up @@ -224,7 +222,7 @@ def plot_ppc(
*_empirical_cdf(obs_vals),
color="k",
linewidth=linewidth,
label="Observed {}".format(var_name),
label="Observed",
drawstyle=drawstyle,
zorder=3
)
Expand Down Expand Up @@ -253,15 +251,15 @@ def plot_ppc(
drawstyle=drawstyle,
linewidth=linewidth
)
ax_i.plot([], color=color, label="Posterior predictive {}".format(pp_var_name))
ax_i.plot([], color=color, label="Posterior predictive")
if mean:
ax_i.plot(
*_empirical_cdf(pp_vals.flatten()),
color=color,
linestyle="--",
linewidth=linewidth * 1.5,
drawstyle=drawstyle,
label="Posterior predictive mean {}".format(pp_var_name)
label="Posterior predictive mean"
)
ax_i.set_yticks([0, 0.5, 1])

Expand All @@ -276,7 +274,7 @@ def plot_ppc(
"linewidth": linewidth * 1.5,
"zorder": 3,
},
label="Posterior predictive mean {}".format(pp_var_name),
label="Posterior predictive mean",
ax=ax_i,
legend=legend,
)
Expand All @@ -290,7 +288,7 @@ def plot_ppc(
hist,
color=color,
linewidth=linewidth * 1.5,
label="Posterior predictive mean {}".format(pp_var_name),
label="Posterior predictive mean",
zorder=3,
linestyle="--",
drawstyle="steps-pre",
Expand All @@ -316,7 +314,7 @@ def plot_ppc(
color="k",
markersize=markersize,
alpha=alpha,
label="Observed {}".format(var_name),
label="Observed",
zorder=4,
)

Expand All @@ -340,9 +338,7 @@ def plot_ppc(
vals, yvals, "o", zorder=2, color=color, markersize=markersize, alpha=alpha
)

ax_i.plot(
[], color=color, marker="o", label="Posterior predictive {}".format(pp_var_name)
)
ax_i.plot([], color=color, marker="o", label="Posterior predictive")

ax_i.set_yticks([])

Expand Down
8 changes: 8 additions & 0 deletions arviz/tests/base_tests/test_plots_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,14 @@ def test_plot_ppc_bad_ax(models, fig_ax):
plot_ppc(models.model_1, ax=ax2)


def test_plot_legend(models):
axes = plot_ppc(models.model_1)
legend_texts = axes.get_legend().get_texts()
result = [i.get_text() for i in legend_texts]
expected = ["Posterior predictive", "Observed", "Posterior predictive mean"]
assert result == expected


@pytest.mark.parametrize("var_names", (None, "mu", ["mu", "tau"]))
def test_plot_violin(models, var_names):
axes = plot_violin(models.model_1, var_names=var_names)
Expand Down