# How to get a az.plot\_trace() legend of multi-dimensional β distribution to show dims and not chains

**URL:** https://discourse.pymc.io/t/how-to-get-a-az-plot-trace-legend-of-multi-dimensional-distribution-to-show-dims-and-not-chains/10555
**Category:** Questions
**Tags:** arviz
**Created:** [October 7, 2022, 4:33pm UTC](https://discourse.pymc.io/t/how-to-get-a-az-plot-trace-legend-of-multi-dimensional-distribution-to-show-dims-and-not-chains/10555 "2022-10-07T16:33:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kraftfaust](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/kraftfaust/32/5642_2.png) [@Kraftfaust](https://discourse.pymc.io/u/Kraftfaust)
#### Post date: [October 7, 2022, 4:33pm UTC](https://discourse.pymc.io/t/how-to-get-a-az-plot-trace-legend-of-multi-dimensional-distribution-to-show-dims-and-not-chains/10555/1 "2022-10-07T16:33:53Z")

</div>

I have hierarchical model and I’m trying to get the legend in a az.plot\_tract() to show the coords variable names, and not the chains.

```auto
az.plot_trace(idata, var_names=['β'],compact = True, combined = True, legend = True, coords={'beta_list': ['Dim1']},figsize = (14,6))

```

where:

```auto
β = pm.Normal("β", mu = mu_β, sigma = sigma_β, dims = ('group','beta_list'))   

```

No matter what I try the legend only show’s the chains.

Any thoughts would be greatly appreciated. Thank you!

---

<div class="post-metadata">

### Author: ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)
#### Post date: [October 10, 2022, 11:00pm UTC](https://discourse.pymc.io/t/how-to-get-a-az-plot-trace-legend-of-multi-dimensional-distribution-to-show-dims-and-not-chains/10555/2 "2022-10-10T23:00:28Z")

</div>

There is a bug in how ArviZ decided to place the multiple legends involved in plot\_trace.

You can reproduce your issue with:

```auto
import arviz as az
idata = az.load_arviz_data("rugby")
az.plot_trace(idata, var_names="atts", compact=True, combined=True, legend=True, show=True)

```

where not team legend is added. One possible workaround is using a scalar variable in the model to generate an extra axes where ArviZ will place the chain one, so in doing that it doesn’t overwrite the team legend.

```auto
az.plot_trace(idata, var_names=["home", "atts"], compact=True, combined=True, legend=True, show=True)

```

so the chain legend is placed in the home variable and then the team legend is correctly added. It is also possible to generate the single plot with something llike:

```auto
_, ax1 = plt.subplots(1, 2)
_, ax2 = plt.subplots(1, 2)
ax = np.vstack((ax1, ax2))
az.plot_trace(idata, var_names=["atts", "home"], compact=True, combined=True, legend=True, axes=ax, show=True)

```

Note the variable order is key to get the desired results and work around the bug.

---

<div class="post-metadata">

### Author: ![Kraftfaust](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/kraftfaust/32/5642_2.png) [@Kraftfaust](https://discourse.pymc.io/u/Kraftfaust)
#### Post date: [October 11, 2022, 11:37pm UTC](https://discourse.pymc.io/t/how-to-get-a-az-plot-trace-legend-of-multi-dimensional-distribution-to-show-dims-and-not-chains/10555/3 "2022-10-11T23:37:35Z")

</div>

Thank you so much for explanation and work-around. Side note - I really enjoyed your ArviZ in depth blog entries. Cheers!
