-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize legend in object interface #3247
Comments
See issue #2994 but tl;dr, legends are a pain and matplotlib does not really have an interface for modifying them. I currently use this function for moving the legend around :
(it is custom made but largely inspired by move_legend) |
I understand. It's unfortunate that mpl makes it so difficult, because
modifying the legend is the one thing I desperately need to do. Otherwise,
the plot object interface is amazing.
…On Tue, Feb 7, 2023 at 6:59 AM thuiop ***@***.***> wrote:
See issue #2994 <#2994> but
tl;dr, legends are a pain and matplotlib does not really have an interface
for modifying them.
I currently use this function for moving the legend around :
def move_legend_fig_to_ax(fig, ax, loc, bbox_to_anchor=None, **kwargs):
if fig.legends:
old_legend = fig.legends[-1]
else:
raise ValueError("Figure has no legend attached.")
old_boxes = old_legend.get_children()[0].get_children()
legend_kws = inspect.signature(mpl.legend.Legend).parameters
props = {
k: v for k, v in old_legend.properties().items() if k in legend_kws
}
props.pop("bbox_to_anchor")
title = props.pop("title")
if "title" in kwargs:
title.set_text(kwargs.pop("title"))
title_kwargs = {k: v for k, v in kwargs.items() if k.startswith("title_")}
for key, val in title_kwargs.items():
title.set(**{key[6:]: val})
kwargs.pop(key)
kwargs.setdefault("frameon", old_legend.legendPatch.get_visible())
# Remove the old legend and create the new one
props.update(kwargs)
fig.legends = []
new_legend = ax.legend(
[], [], loc=loc, bbox_to_anchor=bbox_to_anchor, **props
)
new_legend.get_children()[0].get_children().extend(old_boxes)
(it is custom made but largely inspired by move_legend
<https://seaborn.pydata.org/generated/seaborn.move_legend.html>)
—
Reply to this email directly, view it on GitHub
<#3247 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACHQESS3B2GMMZ2J5FTXNXDWWJBLTANCNFSM6AAAAAAUTOI3RE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Is it possible to customize the legend of an object interface? Right now I'm trying a variety of ways to access it via the matplotlib axes and figures, but nothing changes the legend.
g = (so.Plot(ange_surv, x='Week ', y='GWC',
color='Treatment ')
.add(so.Dot(alpha = 0.35), so.Jitter(), so.Dodge())
.add(so.Range(linewidth=2), so.Est(errorbar=('se', 1)), so.Dodge())
.add(so.Dot(pointsize=12), so.Agg(), so.Dodge())
.label(x='Week of Experiment', y='GWC')
.theme(sns_theme)
)
f = plt.figure()
res = g.on(f).plot()
ax = f.axes[0]
ax.legend(ncols=2, loc=2, bbox_to_anchor=(1.01, 1.01))
plt.show()
And the matplotlib rcparams don't allow for fine-tuning.
The text was updated successfully, but these errors were encountered: