简体   繁体   中英

Legend size/marker in relplot seaborn

I've been looking in SO for increasing legend/hue size in relplot.

plt.rcParams["axes.labelsize"] = 20
g = sns.relplot(x='Time(days)', y='Duration Total (s)', hue='Outcome', data=t1,height=15, aspect=1, s=50);
plt.suptitle("a_10",fontsize=25, fontweight='bold')

I can't seem to wrap my heads around it. There're so many mixed references, it's all a bit confusing.

I've spent some time digging through this, an yes, you're right to say it's confusing.

I will also assume you are talking about text size, and not marker size.

Ways that work

There are two main ways I would suggest you to increase the legend size (retrieved from here ):

  • Scaling up the font globally using sns.set() . For example:
sns.set(font_scale=1.5)
# plotting code here...
  • Scaling up the font locally using sns.plotting_context() . For example:
with sns.plotting_context("notebook", font_scale=1.5):
    # plotting code here...

The problem with both approaches is that they also increase the size of other elements . So, for example, axis labels will grow alongside the legend:

Way that doesn't work

In the mentioned SO link , there's also an answer that addresses directly modifying the legend. It uses the private property _legend of the FacetGrid and increase the text size directly:

g = sns.relplot(x='sepal_length', y='sepal_width', hue='species', data=iris)
plt.setp(g._legend.get_texts(), fontsize=16)

This method, however, severely messes up with the formatting. From a quick glance, I think it happens because FacetGrid calculates its size using the legend dimensions. So, altering the legend afterwards messes things up.

What to do?

From my research, it looks like there's no simple way to do what you want. You can submit an issue to the seaborn repository and maybe they will fix it (you can give a reference to your question). More hopefully, there is a way to do it and they will simply point out how.

Good luck :)

If anyone still stumbles across this post, here is a solution that worked for me:

How to set legend marker size and alpha?

The required code being:

for lh in g._legend.legendHandles: 
    lh.set_alpha(1)
    lh._sizes = [50] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM