Can't move arviz legend

Hello. I’m having trouble moving a legend in a plot generated in arviz. Say I have these two traces:

import pymc as pm
import arviz as az 

with pm.Model():
    pm.Normal("a", 0, 1)
    trace1 = pm.sample()
    trace2 = pm.sample()

And I want to compare them with arviz:

axes = az.plot_forest((trace1, trace2), combined=True)


If I want to move the legend I would normally do:

ax = axes[0]
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels, loc="upper left", bbox_to_anchor=(1, 1))

but handles and labels are both empty, so this doesn’t work

print(handles, labels)
[] []

(This is a toy example. I have a plot comparing 3 models with many variables where the legend gets put right in the middle of the plot which makes the plot unreadable).

Any help would be great, thanks!

I’m using arviz 0.15.1.

I managed to solve this issue using the following code:

legend = ax[0].get_legend()
labels = [] if legend is None else [str(x._text) for x in legend.texts]
handles = [] if legend is None else legend.legend_handles

Solution found here: Codereview question

1 Like