Search code examples
pythonseabornlegendscatter-plot

How to adjust the size of the dots in the legend of a Seaborn scatterplot?


I know that the s argument in searbons scatterplot allows to control the size of the dots. For instance:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['Y_LOCATION'] = [1, 1, 1, 2, 2, 4]
df['X_LOCATION'] = [1, 2, 3, 4, 3, 1]
df['VALUE'] = [-0.45, -0.14, -0.12, -0.36, -0.48, -0.20]
sns.set(rc={'figure.figsize':(40,20)})
sns.set(font_scale=2)
sns.scatterplot(df.Y_LOCATION, df.X_LOCATION, df.VALUE, s=800, palette = "Greens_r")

enter image description here

However, this parameter appears to have no impact on the size of the dots shown in the legend. How can these be adjusted?


Solution

  • You can increase the size of those dots by getting the handles of the dots in the legend, and calling set_sizes() on them. They are [36] by default, by multiplying that by 10, it increases the area (not diameter) by a factor of 10.

    Example:

    ax = sns.scatterplot(x = df.Y_LOCATION, y = df.X_LOCATION, hue = df.VALUE, s=800, palette = "Greens_r")
    handles, labels = ax.get_legend_handles_labels()
    for dot in handles:
        dot.set_sizes(dot.get_sizes() * 10)
    plt.legend(handles, labels)
    

    See also: How to adapt too large dot sizes in a seaborn scatterplot legend?

    Documentation on PathCollection.set_sizes(): https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.PathCollection.set_sizes