Skip to content

Commit

Permalink
Merge pull request #43 from sciris/fix-stacked-separate-legends
Browse files Browse the repository at this point in the history
Fixes for separate legends on stacked plots
  • Loading branch information
cliffckerr committed Nov 23, 2018
2 parents c5a73de + 9994bf3 commit 741dfd6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions sciris/sc_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,19 @@ def separatelegend(ax=None, handles=None, labels=None, reverse=False, figsetting
# can only appear in one figure. Thus, if the legend is in a different figure, the
# object cannot be shown in both the original figure and in the legend. Thus we need
# to copy the handles, and use the copies to render the legend
handles = [ut.cp(x) for x in handles]

handles2 = []
for h in handles:
h2 = ut.cp(h)
h2.axes = None
h2.figure = None
handles2.append(h2)

# Reverse order, e.g. for stacked plots
if reverse:
handles = handles[::-1]
handles2 = handles2[::-1]
labels = labels[::-1]

# Plot the new legend
ax.legend(handles=handles, labels=labels, **l_settings)
ax.legend(handles=handles2, labels=labels, **l_settings)

return fig

0 comments on commit 741dfd6

Please sign in to comment.