代码之家  ›  专栏  ›  技术社区  ›  Sheldon

如何调整Seaborn散点图传说中的点的大小?

  •  0
  • Sheldon  · 技术社区  · 3 年前

    我知道 s 西尔本斯的论点 scatterplot 允许控制点的大小。例如:

    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

    但是,此参数似乎对图例中显示的点的大小没有影响。如何调整?

    1 回复  |  直到 3 年前
        1
  •  1
  •   Nick ODell    3 年前

    您可以通过获取图例中的点的句柄来增加这些点的大小,并调用 set_size() 在他们身上。默认情况下,它们是[36],通过将其乘以10,面积(而不是直径)将增加10倍。

    示例:

    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)
    

    另请参见: How to adapt too large dot sizes in a seaborn scatterplot legend?

    PathCollection.set_size()上的文档: https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.PathCollection.set_sizes

    推荐文章