如何设置图例标记大小和Alpha? [英] How to set legend marker size and alpha?

查看:92
本文介绍了如何设置图例标记大小和Alpha?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

解决方案

您可以通过设置图例标记本身的alpha值来实现.您还可以使用 _sizes 在相同的for循环中设置标记大小:

  n_group = 4000pos = np.concatenate((np.random.randn(n_group,2)+ np.array([-1,-1]),np.random.randn(n_group,2)+ np.array([0.2,1.5]),np.random.randn(n_group,2)+ np.array([0.6,-1.8])))df = pd.DataFrame({"x":pos [:,0],"y":pos [:, 1],标签":np.repeat(range(3),n_group)})g = sns.lmplot("x","y",df,色相=标签",fit_reg = False,大小= 8,scatter_kws = {"alpha":0.1})g._legend.set_title(集群")对于g._legend.legendHandles中的lh:lh.set_alpha(1)lh._sizes = [50]#您也可以使用lh.set_sizes([50]) 

I have a seaborn scatter plot (lmplot) with over 10K points. In order to perceive all the data, it works better when the plot size is larger (making the markers relatively small) and the alpha on the markers is low. However, this makes the markers on the legend difficult to distinguish. How does one set the marker size and marker alpha in Seaborn?

I see that g._legend has a markersize attribute, but directly setting it doesn't do anything.

Example

import numpy as np
import pandas as pd
import seaborn as sns

n_group = 4000

pos = np.concatenate((np.random.randn(n_group,2) + np.array([-1,-1]),
                      np.random.randn(n_group,2) + np.array([0.2, 1.5]),
                      np.random.randn(n_group,2) + np.array([0.6, -1.8])))
df = pd.DataFrame({"x": pos[:,0], "y": pos[:, 1], 
                   "label": np.repeat(range(3), n_group)})

g = sns.lmplot("x", "y", df, hue = "label", fit_reg = False, 
               size = 8, scatter_kws = {"alpha": 0.1})
g._legend.set_title("Clusters")

解决方案

You can do this by setting the alpha values of the legend markers themselves. You can also use _sizes to set the marker sizes in the same for loop:

n_group = 4000

pos = np.concatenate((np.random.randn(n_group,2) + np.array([-1,-1]),
                      np.random.randn(n_group,2) + np.array([0.2, 1.5]),
                      np.random.randn(n_group,2) + np.array([0.6, -1.8])))
df = pd.DataFrame({"x": pos[:,0], "y": pos[:, 1], 
                   "label": np.repeat(range(3), n_group)})

g = sns.lmplot("x", "y", df, hue = "label", fit_reg = False, 
               size = 8, scatter_kws = {"alpha": 0.1})
g._legend.set_title("Clusters")

for lh in g._legend.legendHandles: 
    lh.set_alpha(1)
    lh._sizes = [50] 
    # You can also use lh.set_sizes([50])

这篇关于如何设置图例标记大小和Alpha?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆