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.
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")
To change the size of the legend text, we pass prop dict with size key as a argument to the legend function.
Size in points^2 markersize'] ** 2. This can be taken literally. In order to obtain a marker which is x points large, you need to square that number and give it to the s argument. So the relationship between the markersize of a line plot and the scatter size argument is the square.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With