Repository: arviz-devs/arviz

Issue:
`plot_density` does not work for models with different variables
**Describe the bug**
When passing data of several models to `plot_density`, it throws an error if both models contain parameters that the other model does not have.
As long the the variables in the one model are a subset of the the variables of the other model it works.
I think that the problem lies in how `plot_density` determines the number of plots to create. It calculates the number of parameters in each model and then uses the maximum:
```
192     length_plotters = []
193     for plotters in to_plot:
194         length_plotters.append(len(plotters))
195         for var_name, selection, _ in plotters:
196             label = make_label(var_name, selection)
197             if label not in all_labels:
198                 all_labels.append(label)
199     length_plotters = max(length_plotters)
```
That does not account for the situation where the union of parameters over all models is larger than the set parameters in each single model.

**To Reproduce**
This simple example should demonstrate what I mean:
```python
import numpy as np
import xarray as xr
import arviz

n_draws = 1000

model_ab = xr.Dataset({
    "a": ("draw", np.random.normal(size=n_draws)),
    "b": ("draw", np.random.normal(size=n_draws)),
})
model_b = xr.Dataset({
    "b": ("draw", np.random.normal(size=n_draws)),
})
model_bc = xr.Dataset({
    "c": ("draw", np.random.normal(size=n_draws)),
    "b": ("draw", np.random.normal(size=n_draws)),
})

# Works
arviz.plot_density([model_ab, model_b], data_labels=["ab", "b"]);

# Does not work
arviz.plot_density([model_ab, model_bc], data_labels=["ab", "bc"]);
```

**Expected behavior**
In the second case, the code should create 3 subplots, for parameters a, b, and c. While the plots for a and c would contain only one density, the plot for b would contain two densities.

**Additional context**
arviz Version: 0.6.1



Discussion:
Thanks for reporting! Also feel free to reach out if you'd like to make a PR to fix it an need some guidance

Resolving patch:
diff --git a/arviz/plots/densityplot.py b/arviz/plots/densityplot.py
--- a/arviz/plots/densityplot.py
+++ b/arviz/plots/densityplot.py
@@ -196,7 +196,7 @@ def plot_density(
             label = make_label(var_name, selection)
             if label not in all_labels:
                 all_labels.append(label)
-    length_plotters = max(length_plotters)
+    length_plotters = len(all_labels)
     max_plots = rcParams["plot.max_subplots"]
     max_plots = length_plotters if max_plots is None else max_plots
     if length_plotters > max_plots:


Test patch:
diff --git a/arviz/tests/base_tests/test_plots_bokeh.py b/arviz/tests/base_tests/test_plots_bokeh.py
--- a/arviz/tests/base_tests/test_plots_bokeh.py
+++ b/arviz/tests/base_tests/test_plots_bokeh.py
@@ -90,6 +90,14 @@ def test_plot_density_discrete(discrete_model):
     assert axes.shape[0] == 1
 
 
+def test_plot_density_no_subset():
+    """Test plot_density works when variables are not subset of one another (#1093)."""
+    model_ab = from_dict({"a": np.random.normal(size=200), "b": np.random.normal(size=200),})
+    model_bc = from_dict({"b": np.random.normal(size=200), "c": np.random.normal(size=200),})
+    axes = plot_density([model_ab, model_bc])
+    assert axes.shape[0] == 3
+
+
 def test_plot_density_bad_kwargs(models):
     obj = [getattr(models, model_fit) for model_fit in ["model_1", "model_2"]]
     with pytest.raises(ValueError):
diff --git a/arviz/tests/base_tests/test_plots_matplotlib.py b/arviz/tests/base_tests/test_plots_matplotlib.py
--- a/arviz/tests/base_tests/test_plots_matplotlib.py
+++ b/arviz/tests/base_tests/test_plots_matplotlib.py
@@ -112,6 +112,14 @@ def test_plot_density_discrete(discrete_model):
     assert axes.shape[0] == 2
 
 
+def test_plot_density_no_subset():
+    """Test plot_density works when variables are not subset of one another (#1093)."""
+    model_ab = from_dict({"a": np.random.normal(size=200), "b": np.random.normal(size=200),})
+    model_bc = from_dict({"b": np.random.normal(size=200), "c": np.random.normal(size=200),})
+    axes = plot_density([model_ab, model_bc])
+    assert axes.shape[0] == 3
+
+
 def test_plot_density_bad_kwargs(models):
     obj = [getattr(models, model_fit) for model_fit in ["model_1", "model_2"]]
     with pytest.raises(ValueError):