Repository: arviz-devs/arviz

Issue:
plot_posterior doesn' accept label as kwarg
I think this is related to how we pass kwargs downstream to plot_dist or plot_kde... will look into it at some point unless someone beats me to it. My hunch is that plot_dist and plot_kde have a `label` argument and pass it explicitly to matplotlib instead of doing `setdefault` so if a label is passed via kwargs too it becomes a duplicated argument and errors out

Fixing this would allow using plot_posterior to overlay multiple distributions while having a legend.


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs) Report
> Merging [#1710](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs) (2b1b1ec) into [main](https://codecov.io/gh/arviz-devs/arviz/commit/1997afe0cf92d32e6ee71a94226fc458ace22313?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs) (1997afe) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/arviz-devs/arviz/pull/1710/graphs/tree.svg?width=650&height=150&src=pr&token=KZTdQCEf7t&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs)](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs)

```diff
@@           Coverage Diff           @@
##             main    #1710   +/-   ##
=======================================
  Coverage   90.86%   90.86%           
=======================================
  Files         108      108           
  Lines       11821    11823    +2     
=======================================
+ Hits        10741    10743    +2     
  Misses       1080     1080           
```


| [Impacted Files](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs) | Coverage Δ | |
|---|---|---|
| [arviz/plots/backends/matplotlib/kdeplot.py](https://codecov.io/gh/arviz-devs/arviz/pull/1710/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs#diff-YXJ2aXovcGxvdHMvYmFja2VuZHMvbWF0cGxvdGxpYi9rZGVwbG90LnB5) | `98.87% <100.00%> (+0.02%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs). Last update [1997afe...2b1b1ec](https://codecov.io/gh/arviz-devs/arviz/pull/1710?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=arviz-devs).

utkarsh-maheshwari: Sure, I'll keep this in mind. Thanks for pointing it out. 
I'll try to do it for bokeh as well.
utkarsh-maheshwari: Not sure if it is to be done for contour_kwargs or fill_kwargs.
I guess since there is a loop to plot contour, `label` shouldn't be added there. Same applies to fill_kwargs.
utkarsh-maheshwari: Unlike matplotlib, passing `legend_label=None` generates an error in case of bokeh.
utkarsh-maheshwari: Errors are because `label` is still unused in bokeh plot_kde. When passed `label` in bokeh plot_kde, it goes into `plot_kwargs`. 
utkarsh-maheshwari: https://dev.azure.com/ArviZ/ArviZ/_build/results?buildId=4434&view=logs&j=5009c7ae-01dd-59da-6106-f3ac988cc395&t=0d1d07e1-2d84-5a43-33b6-ac11fbcc1860&l=94

Are these errors related to my code changes??
OriolAbril: Do you want to add it to the changelog?
utkarsh-maheshwari: Done!
OriolAbril: xarray has modified how selection works internally so now what used to be a ValueError is a KeyError. Not completely sure what would be the best way forward but this is what's breaking the test
utkarsh-maheshwari: Okayy. Wondering if changing the `ValueError` to https://github.com/arviz-devs/arviz/blob/33ec4e1582ab7b9c80841140fc3dcf004532db05/arviz/tests/base_tests/test_plot_utils.py#L145 to `KeyError` would work 🤔 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9b57b79..19b58b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,8 @@
 * Renamed `sample` dim to `__sample__` when stacking `chain` and `draw` to avoid dimension collision ([1647](https://github.com/arviz-devs/arviz/pull/1647))
 * Removed the `circular` argument in `plot_dist` in favor of `is_circular` ([1681](https://github.com/arviz-devs/arviz/pull/1681))
 * Fix `legend` argument in `plot_separation` ([1701](https://github.com/arviz-devs/arviz/pull/1701))
+* Removed testing dependency on http download for radon dataset ([1717](https://github.com/arviz-devs/arviz/pull/1717))
+* Fixed plot_kde to take labels with kwargs. ([1710](https://github.com/arviz-devs/arviz/pull/1710))
 
 ### Deprecation
 * Deprecated `index_origin` and `order` arguments in `az.summary` ([1201](https://github.com/arviz-devs/arviz/pull/1201))
diff --git a/arviz/plots/backends/bokeh/kdeplot.py b/arviz/plots/backends/bokeh/kdeplot.py
index 907d4e2..ce83c70 100644
--- a/arviz/plots/backends/bokeh/kdeplot.py
+++ b/arviz/plots/backends/bokeh/kdeplot.py
@@ -147,6 +147,8 @@ def plot_kde(
                     patch = ax.patch(patch_y, patch_x, **fill_kwargs)
                 glyphs.append(patch)
 
+            if label is not None:
+                plot_kwargs.setdefault("legend_label", label)
             if not rotated:
                 line = ax.line(x, density, **plot_kwargs)
             else:
diff --git a/arviz/plots/backends/matplotlib/kdeplot.py b/arviz/plots/backends/matplotlib/kdeplot.py
index baf54fb..ed3f2ca 100644
--- a/arviz/plots/backends/matplotlib/kdeplot.py
+++ b/arviz/plots/backends/matplotlib/kdeplot.py
@@ -144,11 +144,13 @@ def plot_kde(
         else:
             fill_kwargs.setdefault("alpha", 0)
             if fill_kwargs.get("alpha") == 0:
-                ax.plot(x, density, label=label, **plot_kwargs)
+                label = plot_kwargs.setdefault("label", label)
+                ax.plot(x, density, **plot_kwargs)
                 fill_func(fill_x, fill_y, **fill_kwargs)
             else:
+                label = fill_kwargs.setdefault("label", label)
                 ax.plot(x, density, **plot_kwargs)
-                fill_func(fill_x, fill_y, label=label, **fill_kwargs)
+                fill_func(fill_x, fill_y, **fill_kwargs)
         if legend and label:
             ax.legend()
     else:


Test patch:
diff --git a/arviz/tests/base_tests/test_plots_bokeh.py b/arviz/tests/base_tests/test_plots_bokeh.py
index 5e3a963..16d1a3d 100644
--- a/arviz/tests/base_tests/test_plots_bokeh.py
+++ b/arviz/tests/base_tests/test_plots_bokeh.py
@@ -992,7 +992,7 @@ def test_plot_ppc_ax(models, kind):
         {"point_estimate": "mode"},
         {"point_estimate": "median"},
         {"point_estimate": None},
-        {"hdi_prob": "hide"},
+        {"hdi_prob": "hide", "legend_label": ""},
         {"ref_val": 0},
         {"ref_val": None},
         {"ref_val": {"mu": [{"ref_val": 1}]}},
diff --git a/arviz/tests/base_tests/test_plots_matplotlib.py b/arviz/tests/base_tests/test_plots_matplotlib.py
index 5ae9823..1ddfa36 100644
--- a/arviz/tests/base_tests/test_plots_matplotlib.py
+++ b/arviz/tests/base_tests/test_plots_matplotlib.py
@@ -927,7 +927,7 @@ def test_plot_rank(models, kwargs):
         {"rope": {"mu": [{"rope": (-2, 2)}], "theta": [{"school": "Choate", "rope": (2, 4)}]}},
         {"point_estimate": "mode"},
         {"point_estimate": "median"},
-        {"hdi_prob": "hide"},
+        {"hdi_prob": "hide", "label": ""},
         {"point_estimate": None},
         {"ref_val": 0},
         {"ref_val": None},