'How to set legend marker size and alpha?

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")

Scatter plot of three dense clusters of points, with different colors for each cluster. The cluster colors are easily distinguished in the plot, but the markers in the legend are barely visible.



Solution 1:[1]

the above did not work for me in a seaborn lineplot. This did:

g = sns.lineplot(data=df, x='X', y='Y', hue='HUE', ci=False, style='STYLE',
             markers=True, ms=16, dashes=False)

#get legend and change stuff
handles, lables = g.get_legend_handles_labels()
for h in handles:
    h.set_markersize(10)

# replace legend using handles and labels from above
lgnd = plt.legend(handles, lables, bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0, title='TITLE')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maarten