简体   繁体   English

relplot seaborn 中的图例大小/标记

[英]Legend size/marker in relplot seaborn

I've been looking in SO for increasing legend/hue size in relplot.我一直在寻找 SO 以增加 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() .使用sns.set()全局缩放字体。 For example:例如:
sns.set(font_scale=1.5)
# plotting code here...
  • Scaling up the font locally using sns.plotting_context() .使用sns.plotting_context()在本地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.在提到的 SO链接中,还有一个解决直接修改图例的答案。 It uses the private property _legend of the FacetGrid and increase the text size directly:它采用了私有财产_legend的的FacetGrid并直接增加了文字大小:

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.乍一看,我认为这是因为FacetGrid使用图例尺寸计算其大小。 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).您可以向seaborn 存储库提交问题,也许他们会修复它(您可以参考您的问题)。 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? 如何设置图例标记大小和 alpha?

The required code being:所需的代码是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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